по нижеследующей таблице должен собрать в один массив ID всех детей. Когда вызываю функцию index() результат должен быть: [1,2,3,4,5,6]; В чем тут моя ошибка ? Заранее спасибо за помощь.
// Мой контроллер
class MenuController extends Controller
{
public function index()
{
$id = 0;
$menu = Menu::where('id', $id)->with('children')->first();
$array = array();
$a = $this->menus($menu, $array);
dd($a);
}
public function menus($menu, $array)
{
array_push($array, $menu->id);
if (count($menu->children->where('parent_id', $menu->id))) {
foreach ($menu->children->where('parent_id', $menu->id) as $menu) {
$this->menus($menu, $array);
}
}
return $array;
}
}
// Модель
class Menu extends Model
{
public function parent(){
return $this->belongsTo(Menu::class);
}
public function children(){
return $this->hasMany(self::class,'parent_id');
}
}