【问题标题】:How to display default image if no filename exists in Laravel database如果 Laravel 数据库中不存在文件名,如何显示默认图像
【发布时间】:2020-03-25 20:16:32
【问题描述】:

我想为帖子列表中的每个条目显示一张图片。图像的名称是唯一的文件名。它们都保存在数据库(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

这是我收到的错误消息: “试图获取非对象的属性”

【问题讨论】:

  • 试试这个$image = Vehiclefulldataimage::where('vehiclefulldatas_id', $id)-&gt;first(); if(!$image){ $image = 'path/to/your/default/image' }

标签: laravel


【解决方案1】:

试试下面的代码: 您使用的是赋值运算符而不是比较运算符

    //@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

【讨论】:

    【解决方案2】:

    检查你的 if 语句......你在做什么是一个赋值而不是一个条件测试......你可以包含一个列......带有一个指向默认图像路径的字符串......并设置它默认...然后加载它...作为默认图像

    【讨论】:

      【解决方案3】:

      以上所有答案都可以解决您的问题。但是,我建议你使用 Laravel 的特性 Mutators 来解决这个问题。

      https://laravel.com/docs/5.8/eloquent-mutators

      Laravel mutators 允许你格式化或修改你的 Model 属性。

      只需在您的 Vehiclefulldataimage 模型中编写以下代码:

      public function getImageAttribute($value) 
      {
           //can write more logic here.
           return $value ?: 'path/to/your/default/image';
      }
      

      如果条件刀片模板,您就不必再关心了。如果您的图像为空,则 mutator 将返回默认图像。因此,当您检索 Vehiclefulldataimage 实例时,突变器在任何地方都能正常工作

      【讨论】:

        【解决方案4】:

        谢谢大家。 在查看了所有答案之后,我对我的代码做了一个简单的修改。我改变了: @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
        

        【讨论】:

          猜你喜欢
          • 2018-03-11
          • 2013-11-24
          • 2012-07-23
          • 1970-01-01
          • 1970-01-01
          • 2015-08-10
          • 2013-01-10
          • 2023-03-31
          • 2012-01-27
          相关资源
          最近更新 更多