If you are going to be selecting only one image type at a time, you can add a where clause to the with() method:
$imageTypeId = 'some id you need to set';
$product = Product::with(['images' => function($query) use ($imageTypeId)
{
$query->where('id_imagetype', $imageTypeId);
}])->where('id_product', '=', Request::segment(2))->get();
If you are going to be loading a product with both image types, but you want them grouped together by type, it's probably better to do something like this:
class Product extends Eloquent {
protected $table = 'products';
public function layoutImages()
{
return $this->hasMany('Productimages', 'id_product')->where('id_imagetype', 1);
}
public function contentImages()
{
return $this->hasMany('Productimages', 'id_product')->where('id_imagetype', 2);
}
}
$product = Product::with(['layoutImages', 'contentImages'])->where('id_product', '=', Request::segment(2))->get();
Thank you for fast response. The first proposed solution I already implemented but was not what I needed to achieve but second one is just what I was looking for. Thank you very much!
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community