php - Laravel - how to list all keys on relation in collection? -


i have like:

$categories = categories::with('sections')->get(); 

it gives me collection:

img

as can see on image can access "y"'es by

dd($categories->pluck('id')); 

which gives me wanted, id's of items in collection

enter image description here

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

Popular posts from this blog

python - Selenium remoteWebDriver (& SauceLabs) Firefox moseMoveTo action exception -

html - How to custom Bootstrap grid height? -

transpose - Maple isnt executing function but prints function term -