简体   繁体   中英

Attempt to read property "image" on bool in Laravel

I am new to Laravel Plz help me to solve this problem.

I have multiple images of a single product in the Database and want to show them in a carousel in foreach loop.

i also have the product id and images in a database table.

My Function

public function view_product_details($id)
{
    $product_details_images = product_attr_images::where('product_id', $id)->first();
    // dd($product_details_images);


    return view('frontend.view_product_details', compact( 'product_details_images'));
}

My View File

<div class="carousel-inner border">
    @foreach ($product_details_images as $key=> $images)       
        <div class="carousel-item{{$key==0 ? 'active' : ''}} ">
            <img class="w-100 h-100" src="product/{{$images->image}}" alt="Image">
        </div>
    @endforeach
</div>

I want to fetch all images and show them in the carousel in foreach loop but I can't. i am facing error of images in bool. I don't know what is happening.

plz help me. I will be very thankful to you

  1. Please follow Laravel's naming conventions. They are not only easier for us to read, but also used by Laravel to perform to many magical operations under the hood.

  2. You see one image because you used first() method which returns the first matched instance. In order to fetch all matched instances in a collection, use get().

product_attr_images::where('product_id', $id)->get();
  1. I didn't understand your desing. Your page will only have the images, and display them in carousel, right? If this is the case, your blade file should work, but you should add a space between carousel-item and {{$key==0. If this isn't the case, you should pass the images as a part of a bigger data set from controller to the product page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM