jeudi 30 mai 2019

Why is lazy loading nested relation for each item in collection end up loading same values as last item's relation?

When I lazy load a relation for every item in collection based on some condition, all items end up having the same relation loaded. That loaded relation for all items is the last item's relation.

I tried setting custom attribute like

$single->parent->filenames = Model::where('id', $id);

but it doesn't work either. Here is my code

$index->transform(function($single) use (&$i){

            if($single->content_type === 'type1') {

                $single->owner->parent->load('fileNames');

            } else if($single->content_type === 'type2') {

                //     $single->parent->file_names = Model::where('id', $single->owner->id);

                $single->owner->parent->load(['fileNames' => function($q) use ($single) {
                    $q->where('file_names.id', $single->owner->id);
                }]);

            }

            return $single;
        });

In case of 'type2', I want $single->parent->file_names to be having one entry with id that is given in where clause

But What I get is all items in collection ends up having the same value of parent->file_names attribute as the collection's last item's parent->file_names attribute.



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2XdgQh7
via IFTTT

Aucun commentaire:

Enregistrer un commentaire