Reputation: 221
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:
Upvotes: 1
Views: 1382
Reputation: 57141
$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',
]);
Upvotes: 1
Reputation: 77063
menu is returning the result of a menu model's toTree method. As we can see, it returns an array. Since an array does not have an ->add()
method, you get the error mentioned in the question. However, you can get a class for your menumodel via
$menuClass = config('admin.database.menu_model');
/** @var Menu $menuModel */
$menuModel = new $menuClass();
You will need to find out what your admin.database.menu_model
is in your case and make sure you add your menu as desired. You will most probably have to call $menuModel::create
Upvotes: 1