Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Bahjaat's avatar
Level 26

Anyone considered 'pluck_deep' with a 'Collection'?

Maybe an example is better in this case.

$result = Model::with('relation1.relation2.relation3')->get();

What about when you are interested in all of the entries from 'relation3'?

I'm thinking about a macro with the 'Collection' class.

Collection::macro('pluck_deep', function($arg) {
    
    $relations = explode('.', $arg);
    for ($i = 0; $i < $count; $i++) {
        // ...
    }

});

$result->pluck_deep('relation1.relation2.relation3');

Has someone did this before or has any ideas? Thanks.

0 likes
3 replies
jlrdw's avatar

Why, when you can return results with eloquent.

Bahjaat's avatar
Level 26

Relation1 and 2 can be hasMany. Has to loop through them also to get all Relation3 rows.

Bahjaat's avatar
Level 26

Solved. Queried the other way around indeed. Thanks.

Please or to participate in this conversation.