php - Laravel - how to list all keys on relation in collection? -
i have like:
$categories = categories::with('sections')->get(); it gives me collection:
as can see on image can access "y"'es by
dd($categories->pluck('id')); which gives me wanted, id's of items in collection
but question is
how id's of "y" ? attributes of relationship belongs each of these items
and don't want foreaching. <---- know how way.
but there must better way, faster.
how achieve without needless foreaching collection on , on :)?
you can retrieve ids in sections using dot notations using example below:
you have:
$categories = categories::with('sections')->get(); getting ids of sections be:
$sections = $categories->pluck('sections.*.id')->flatten()->values(); this access collections based on depth, sections-all-ids. , return reindexed underlying array.
the same if need sections, can stop @ sections.
ps: @jerodev mentioned, collections wraps php array in more useful ways, , underlying pluck , many of these operations use loops.
hope helpful


Comments
Post a Comment