panthro panthro - 3 months ago 24
PHP Question

Flatten array in Laravel?

I need to get an array of a list of random ids from a model, I do:

User::all('id')->random(5)->flatten()->toArray()


But this still out a multi array:

0 => array:1 [
"id" => 20
]
1 => array:1 [
"id" => 69
]
....


Im looking for something like:

[20, 69]

Answer

Try pluck():

User::all('id')->random(5)->pluck('id')->toArray();
Comments