Thanks @michaloravec Yeah that was my bad a wrote it incorrectly in my question but of course i used belongsToMany on both models in my projects.
However now as you can see i load productimges as well but i would like to load only the image with featured field is 1. and select only the image column.
It works
$products = Product::whereHas('categories', function ($query) use ($id){
$query->where('categories.id', $id);
})
->with(['productimages' => function($q){
$q->where('productimages.featured', '=', '1');
}, 'categories'])->orderBy('id', 'DESC')
->paginate(9);
but when i try to select only the image column then it is not working
$products = Product::whereHas('categories', function ($query) use ($id){
$query->where('categories.id', $id);
})
->with(['productimages' => function($q){
$q->where('productimages.featured', '=', '1')->select('image);
}, 'categories'])->orderBy('id', 'DESC')
->paginate(9);