untymage's avatar

How can i load a relationship on Illuminate\Support\Collection

i want to load tags relation on the chain for threadmodel :

$this->groups->pluck('thread')->load('tags')->pluck('tags.*.name')->collapse()->toArray();

after first pluck :

img

But it says:

Method Illuminate\Support\Collection::load does not exist.

I know i can add $with propety to my model but i would like to have that on the chain.

How can i have a Illuminate\Database\Eloquent\Collection instead of Illuminate\Support\Collection ?

0 likes
3 replies
Talinon's avatar

@untymage If you only want an array of tag names:

$this->groups->tags->pluck('name')->toArray();
MikeMacDowell's avatar
Level 25

Load the relationships before the pluck. All of the collection methods return the Illuminate\Support\Collection class because they are defined on that class.

This should work:

$this->groups->load('thread.tags')->pluck('thread.*.tags.*.name');

Please or to participate in this conversation.