【问题标题】:Displaying image from database in grid - laravel在网格中显示数据库中的图像 - laravel
【发布时间】:2018-02-20 10:47:30
【问题描述】:

我的数据库中有我的图像path,现在,我正试图将图像放入我的 html 网格中。但是当我尝试获取图像时,我得到一个错误

此集合实例上不存在属性 [路径]。

但是,我的数据库表中有路径属性。一定是我的模型有错误吗?我的模型对我来说似乎是正确的。无论如何,我不在这里做什么,好吗?

感谢您的关心

HTML

<figure>
    <div class="snipcart-item block">
       <div class="snipcart-thumb">
           <a  href="{{ route('product.view', $product->slug)}}"><img src="{{$product->images->path}}" alt=" " class="img-responsive" /></a>
           <p>{{$product->name}}</p>
           <h4>GH₵ {{ number_format($product->price,2) }} </h4>
       </div>
       <div class="snipcart-details top_brand_home_details">
         <form action="#" method="post">
           <fieldset>

           </fieldset>
         </form>
       </div>
    </div>
</figure>

图片

public function products()
{
     return $this->belongsTo(Product::class);
}

产品图片

public function images()
{
     return $this->hasMany(Image::class);
}

【问题讨论】:

标签: php mysql laravel


【解决方案1】:

你需要创建一个foreach,因为它会给你一个收藏。

因此,尝试将其显示为:

@foreach($product->images as $image) 
   @if($image->path)
     <img src="{{ $image->path }}" alt=" " class="img-responsive" />
   @else
      <p>No image to display!</p>
   @endif
@endforeach

希望对你有帮助!

【讨论】:

  • 条件不起作用。它仍然抛出错误htmlspecialchars() expects parameter 1 to be string,
  • 更新答案,立即查看。
  • 假设是 `@if(!$image->path)` 有效。它显示文本No image display
  • 那么您的图像路径未保存,或者它不正确,或者该图像不存在于该路径!因为这在我的项目中有效!
【解决方案2】:

发生这种情况是因为 Product 模型有许多图像。 为了达到路径属性,您应该这样做:

@foreach($product->images as $image) 
    {{$image->path}} 
@endforeach

或者在您的产品模型中执行此操作并更改您的数据库结构

public function image()
{
   return $this->hasOne(Image::class);
}

希望对你有帮助。

【讨论】:

    【解决方案3】:

    如果您的路径没有任何问题,以下是在 Laravel 刀片模板中显示图像的正确方法。

    <img src="{{URL::asset('directory/name_of_image_file.file_format')}}">
    

    【讨论】:

      【解决方案4】:

      您在 DB images 中没有字段 path 您的代码必须是:

      <img src="{{$product->images->[you field in Db images]}}" alt=" " class="img-responsive" />
      

      【讨论】:

      • 我在他的代码中看到了&lt;img src="{{$product-&gt;images-&gt;path}}"
      • 我有一个名为product_images的表,其中字段path
      【解决方案5】:

      HTML

      @foreach($products as $product)
      <figure>
          <div class="snipcart-item block">
             <div class="snipcart-thumb">
                 <a  href="{{ route('product.view', $product->slug)}}"><img src="{{$product->images->first()->path}}" alt=" " class="img-responsive" /></a>
                 <p>{{$product->name}}</p>
                 <h4>GH₵ {{ number_format($product->price,2) }} </h4>
             </div>
             <div class="snipcart-details top_brand_home_details">
               <form action="#" method="post">
                 <fieldset>
      
                 </fieldset>
               </form>
             </div>
          </div>
      </figure>
      @endforeach
      

      【讨论】:

        【解决方案6】:
        <a  href="{{ route('product.view', $product->slug)}}">
           <img src="{{$product->images->first()->path}}" alt=" " class="img-responsive" />
        </a>
        

        $product-&gt;images 是一个集合,你需要从集合中取出图像。现在我已经使用了first(),它将获取第一张图片,您可能希望根据您的场景循环浏览集合。您可能想阅读有关 here 的答案

        【讨论】:

        • 我收到一条错误消息trying to get property of non-object
        • 在这种情况下,您的图片集似乎是空的,您能dd($product) 告诉我们吗?请同时显示dd($product-&gt;images)
        猜你喜欢
        • 1970-01-01
        • 2015-04-18
        • 2022-01-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-19
        • 2020-05-19
        相关资源
        最近更新 更多