I'm using Laravel 9 with Laravel-Admin v1.8.19.
And I have created successfully some crud operations with Laravel-Admin on a table named overalls
. And here is the resource route according to it at app\Admin\routes.php
:
Route::resource('overalls', OverallController::class);
Now in order to add a new menu item to Laravel-Admin sidebar menu, I tried this:
Admin::routes();
Route::group([
'prefix' => config('admin.route.prefix'),
'namespace' => config('admin.route.namespace'),
'middleware' => config('admin.route.middleware'),
'as' => config('admin.route.prefix') . '.',
], function (Router $router) {
$router->get('/', 'HomeController@index')->name('home');
Route::resource('overalls', OverallController::class);
// Add a new menu item for the overalls CRUD
$menu = \Encore\Admin\Facades\Admin::menu();
$menu->add([
'title' => 'Overalls',
'url' => 'overalls',
'icon' => 'fa-database',
]);
});
But it returns this error:
Call to a member function add() on array
I don't know really what's going wrong here, since I have seen only this for defining a new menu item for admin sidebar.
So if you know how to solve this problem or how to define this new menu item for the sidebar menu, please let me know...
Also this is my route list:
$menu = \Encore\Admin\Facades\Admin::menu();
is giving you an array of menu items. Which is why you get the error. It's not a menu object, more used to display the menu.
If you look at the source of the menu class (https://github.com/z-song/laravel-admin/blob/master/src/Auth/Database/Menu.php) you can see that it's just an eloquent model, the data looks as though it's stored in the admin_menu table on the database.
So you can create extra menu items using something like
$menuModel = config('admin.database.menu_model');
$menuModel::create([
'parent_id' => 0,
'order' => 0,
'title' => 'Overalls',
'url' => 'overalls',
'icon' => 'fa-database',
]);