Flatten array in Laravel?

2019-05-05 21:18发布

问题:

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]

回答1:

Try pluck():

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