judev's avatar
Level 1

Images display with foreach

Hello

I'm trying to display the first occurence of images. but if i'm using a foreach 2 images are displayed than one (because i have a gesture of multiple images upload ).

My PHP code to get all images from database with the get() closure from query builder like this:


Route::get('/home', function () {
        $product = DB::table('products')->paginate(8);
        $image = DB::table('images')->get();
        return view('frontend.home')->with(['products' => $product,'image' => $image]);
    })->name('shop.home');

This is a route to test it ago to implements my logic to controller.

In my blade files:


@if(isset($image))
  @foreach($image as $images)
      @if($p->id === $images->products_id)
          <img class="bd-placeholder-img"src="{{ url('storage/'.$images->path) }}">
      @endif
    @endforeach
@endif

So one of my product are already displayed as well because them have one images but if one of the product have 2 images, the foreach take all images from it.

So how i can display the first occurence of an product image ? I need another table ?

0 likes
7 replies
MichalOravec's avatar
Level 75

@judev Do you have set your relationships? It could be like

$products = Product::with('image')->paginate(8);

Then in view

@foreach ($products as $product)
    <img class="bd-placeholder-img"src="{{ asset("storage/{$product->image->path}") }}">
@endforeach
judev's avatar
Level 1

nope i not have my relationships in it thanks i missed that :)

judev's avatar
Level 1

Ok i have add my relation for it, but now i got an error product_id in images table I have need to add the name of this column in fillable Product models ?

Error :

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'images.product_id' in 'where clause' (SQL: select * from imageswhereimages.product_id in (10, 11))

Edit

I have found my error is because i have add an (s) as product_id like this products_id so thank you i think this will work. i mark this post solved.

1 like
judev's avatar
Level 1

I need to do this

$products->getRelations()['image'][0]->path

to get the path of my image how is the best way to get it ?

if i dd $products that return me all object like the request object in laravel.

MichalOravec's avatar

What do you want to achieve?

If you want to get just one product

$product = Product::with('image')->first();

echo $product->image->path;
judev's avatar
Level 1

Sorry it's me i'm so bad as english :(.

So that i want is:

I have 2 tables one for product and one for multiples_images. I want to display the first of images per products and not all images per products.

I have do the relasionships like this:

//Image.php 

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

//Product.php

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

After that i have do your query with eloquent.

$products = Product::with('image')->get();

But if i want to use your method i nedd to use this

$products->getRelations()['image'][0]->path

Because the dd() return me a big instance like the request instance. with relation

dd($products);

But i can't use this because the collection of instance does not exist because the query is a big instance like the request instance

$product->image->path;

Sorry it's very specific for thumbnails i don't want to display all images in one products box from home page.

I just will like to take the first occurence of product_id them references the id on table products.

judev's avatar
Level 1

so i think i will re-open this post i'm sorry.

Please or to participate in this conversation.