I have to retrieve just an array of id from the given collection, something like [10,54,61,21,etc]. I've tried flatten, pluck, but nothing seems to work apart from a foreach which is something I would like to remove at this step.
// Model
class Children extends Eloquent {
public function directChildrens(){
return $this->hasMany('App\Children','father_id','id')->select('id','father_id');
}
public function childrens(){
return $this->directChildrens()->with('childrens');
}
}
// Controller
return $children->childrens()->get();
As expected it works fine. Here a result:
[{
"id": 10,
"father_id": 2,
"childrens": [
{
"id": 54,
"father_id": 10,
"childrens": [
{
"id": 61,
"father_id": 54,
"childrens": []
}
]
},
{
"id": 21,
"father_id": 10,
"childrens": []
}
]
}]
How can I perform a pluck('id') of this collection in order to get [10,54,61,21] ?