Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

YasseMoh's avatar

joining three tables

I want to show in the user's account the products they 've saved as favourites. I have three tables: users(id, name, pic) products(id, title, user_id) saves(id, user_id, product_id)

in controller public function favourites($account_id) { $savedProducts=DB::table('saves') ->where('saves.user_id',$account_id) ->join('products','products.id','=','saves.products_id') ->join('users','users.id','=','saves.user_id') ->select('products.*','users.pic') ->get();

       return view('favourites',compact('savedProducts'));
   }

in view: @foreach ($savedProducts as $product) {{$product->pic }} @endforeach

everything is ok, but I don't get the pic of the users who uploaded the products. Instead, I get the pic of the user who added these products to their saves(favourites), namely, the user referred to here ( ->where('saves.user_id',$account_id))

I want to show users' pics beside their products

0 likes
3 replies
Snapey's avatar
join('products','products.id','=','saves.products.id') 

you have two dots here. Perhaps you meant;

join('products','products.id','=','saves.products_id') 

BTW, you not liking eloquent?

mterolli's avatar
mterolli
Best Answer
Level 2

@yassemoh , Can you try updating the users join to ->join('users','users.id','=','products.user_id') ? This should show you the picture of the users that users that updated the products.

1 like

Please or to participate in this conversation.