I am not understanding:
when a product is in a folder, there is no need to eager load its image attachments.
I think you need a WHERE and a NOT NULL usage.
Greetings everyone! Thanks in advance for anyone that will take a look.
I have this model Product which in its index I am eager loading its image-attachments. These products also can belong to a folder. In an attempt to reduce the data load, when a product is in a folder, there is no need to eager load its image attachments. My goal is to keep the whole conditional eager loading of the images in the mySQL level and not lazy load it after I get the results. I guess it's a DB::raw that I can't figure it out
So this is the query, as simple as it can get
Product::query()
->select(['id', 'folder_id', 'can_be_sold', 'is_active', 'position', 'title'])
// My thoughts were something like this,
->when(DB::raw('products.folder_id IS NULL'), function ($query) {
$query->with('productAttachments');
})
But it eager loads all products ,even though they are all with their folder_ids filled
Thanks again for your replies!
Please or to participate in this conversation.