Мне нужно получить только массив id из данной коллекции, что-то вроде [10,54,61,21, и т.д.] . Я пробовал flatten , pluck , но, похоже, ничего не работает, кроме foreach , который я хотел бы удалить на этом этапе.
// 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();
Как и ожидалось работает нормально. Вот результат:
[{
"id": 10,
"father_id": 2,
"childrens": [
{
"id": 54,
"father_id": 10,
"childrens": [
{
"id": 61,
"father_id": 54,
"childrens": []
}
]
},
{
"id": 21,
"father_id": 10,
"childrens": []
}
]
}]
Как я могу выполнить pluck ('id') этой коллекции, чтобы получить [10,54,61,21] ?