Support the ongoing development of Laravel.io →
Eloquent Architecture
Last updated 7 months ago.
0

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();
Last updated 7 months ago.
0

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!

Last updated 7 months ago.
0

Sign in to participate in this thread!

Full Stack Europe

Your banner here too?

valuka valuka Joined 5 Mar 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2022 Laravel.io - All rights reserved.