i have two tables :: shops
and attachments
. Shops has all the details for shop and attachments with all the pictures for shop with ref_id
as FK .
now for mobile app i have to list shops with Shop title and one image. I can achieve it using leftjoin as below.
Shop::select('shops.name as shop_name','attachments.name as picture')
->leftjoin('attachments','attachments.ref_id','=','shops.shop_id')
->paginate(10);
This returns just shop name and pictures , I want to achieve same results using relationships that i am not sure of how can i do that. Can someone please advise me on that
Edit
Shop Model
public function attachments(){
return $this->hasMany(Attachment::class, 'ref_id', 'shop_id');
}
dd(Shop::find(34)->attachments()->first());
dd(Shop::with('attachments')->get());
using first dd returns me attachment associated with 34 and but with query is not working and returns just shop