Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果Laravel数据库中不存在文件名,如何显示默认图像_Laravel - Fatal编程技术网

如果Laravel数据库中不存在文件名,如何显示默认图像

如果Laravel数据库中不存在文件名,如何显示默认图像,laravel,Laravel,我想为帖子列表中的每个条目显示一个图像。图像的名称是唯一的文件名。它们都保存在数据库(MySQL)表中。每个帖子都有多张图片。代码正常工作。除非有一篇文章没有图像文件名。这是一种可能的情况,但我无法让代码正常工作。如果帖子没有现有文件名,我想显示一个默认文件名。 这是我的密码: 图像逻辑: /** * Get a vehicle's top or main image data from the image table */ public function t

我想为帖子列表中的每个条目显示一个图像。图像的名称是唯一的文件名。它们都保存在数据库(MySQL)表中。每个帖子都有多张图片。代码正常工作。除非有一篇文章没有图像文件名。这是一种可能的情况,但我无法让代码正常工作。如果帖子没有现有文件名,我想显示一个默认文件名。 这是我的密码:

图像逻辑:

   /**
     * Get a vehicle's top or main image data from the image table
     */
     public function topImage($id)
    {
        $image = Vehiclefulldataimage::where('vehiclefulldatas_id', $id)
              ->orderBy('id', 'ASC')->first();

        return $image;
    }
这是刀片视图,帖子:

    @if (count($posts) > 0)
        @foreach($posts as $post)
            <?php $imageFilename = (new \App\Models\Logic\Images)->topImage($post->id); ?>
            <!--Item-->
            <li>
                <div class="preview">
                    @if ($imageFilename = 0)
                    <img src="{{ asset('images') . '/' . 'defaultImage.jpg' }}" alt="No photo">
                    @else
                    <img src="{{ asset('images') . '/' . $imageFilename->disk_image_filename }}" alt="{{ ucfirst($imageFilename->caption) . ' | ' . $post->title }}">
                    @endif
                </div>
                <div class="desc">
                    <h3>{{ str_limit($post->title, 100, '...') }}</h3>
                </div>       
            </li>
            <!--/Item-->
        @endforeach
    @endif
@if(计数($posts)>0)
@foreach($posts作为$post)
  • @如果($imageFilename=0) @否则 disk_image_filename}“alt=“{{ucfirst($imageFilename->caption)。”|“.$post->title}> @恩迪夫 {{stru_limit($post->title,100,“…”)}
  • @endforeach @恩迪夫
    这是我收到的错误消息:
    “正在尝试获取非对象的属性”

    请检查您的if语句…您正在执行的是赋值而不是条件测试…您可以包含一列..带有指向默认映像路径的字符串..并将其设置为默认..然后将其作为默认映像加载..

    尝试以下代码: 您正在使用赋值运算符而不是比较运算符

        //@if (count($posts) > 0)
    @if (!$posts->isEmpty())
            @foreach($posts as $post)
                <?php $imageFilename = (new \App\Models\Logic\Images)->topImage($post->id); ?>
                <!--Item-->
                <li>
                    <div class="preview">
                    //@if ($imageFilename = 0) this is an assignment operator not comparison operator
                        @if ($imageFilename->isEmpty())
                        <img src="{{ asset('images') . '/' . 'defaultImage.jpg' }}" alt="No photo">
                        @else
                        <img src="{{ asset('images') . '/' . $imageFilename->disk_image_filename }}" alt="{{ ucfirst($imageFilename->caption) . ' | ' . $post->title }}">
                        @endif
                    </div>
                    <div class="desc">
                        <h3>{{ str_limit($post->title, 100, '...') }}</h3>
                    </div>       
                </li>
                <!--/Item-->
            @endforeach
        @endif
    
    /@if(计数($posts)>0)
    @如果(!$posts->isEmpty())
    @foreach($posts作为$post)
    
  • //@如果($imageFilename=0),则这是赋值运算符,而不是比较运算符 @如果($imageFilename->isEmpty()) @否则 disk_image_filename}“alt=“{{ucfirst($imageFilename->caption)。”|“.$post->title}> @恩迪夫 {{stru_limit($post->title,100,“…”)}
  • @endforeach @恩迪夫
    以上所有答案都可以解决您的问题。但是,我建议您通过使用Laravel特性、变体来解决这个问题

    Laravel变体允许您格式化或修改模型属性

    仅在车辆FullDataImage模型中,编写以下代码:

    public function getImageAttribute($value) 
    {
         //can write more logic here.
         return $value ?: 'path/to/your/default/image';
    }
    
    那个么你们就不必在意是否有条件使用刀片模板了。如果图像为空,mutator将返回默认图像。因此,当您检索Vehiclefulldataimage实例时,mutator在任何地方都能正常工作

    谢谢大家。 看了所有答案后,我对代码做了一个简单的修改。我改变了:
    @if($imageFilename=0)

    致:
    @if($imageFilename==null)

    因此,我的代码现在看起来:

    @if (count($posts) > 0)
            @foreach($posts as $post)
                <?php $imageFilename = (new \App\Models\Logic\Images)->topImage($post->id); ?>
                <!--Item-->
                <li>
                    <div class="preview">
                        @if ($imageFilename == null)
                        <img src="{{ asset('images') . '/' . 'defaultImage.jpg' }}" alt="No photo">
                        @else
                        <img src="{{ asset('images') . '/' . $imageFilename->disk_image_filename }}" alt="{{ ucfirst($imageFilename->caption) . ' | ' . $post->title }}">
                        @endif
                    </div>
                    <div class="desc">
                        <h3>{{ str_limit($post->title, 100, '...') }}</h3>
                    </div>       
                </li>
                <!--/Item-->
            @endforeach
        @endif
    
    @if(计数($posts)>0)
    @foreach($posts作为$post)
    
  • @如果($imageFilename==null) @否则 disk_image_filename}“alt=“{{ucfirst($imageFilename->caption)。”|“.$post->title}> @恩迪夫 {{stru_limit($post->title,100,“…”)}
  • @endforeach @恩迪夫
    试试这个
    $image=Vehiclefulldataimage::where('vehiclefulldatas_id',$id)->first();if(!$image){$image='path/to/your/default/image'}