php - Problem with spatie/laravel-menu and spatie/laravel-persmissions
Get the solution ↓↓↓I'm using spatie/laravel-menu and spatie/laravel-persmissions in my laravel project.
I have created a permission, assigned it to a role, and assigned the role to my user. This works fine. Then I have generated a menu the middleware way using a macro like so:
\Menu::macro('main', function () use ($request) {
return \Menu::new()
->withoutWrapperTag()
->withoutParentTag()
->setActiveClassOnLink()
->route('preparation', 'Anstehende Termine')
->route('postprocessing', 'Nachbereitung')
->routeIfCan('administrate', 'protocols', 'Protokolle')
->addItemClass('nav-link')
->setActive($request->url());
});
In my application I have two User models with different connections:App\User;
using connection_a with database db_a andApp\DirectoryA\User;
using connection_b with database db_b
In the auth config the first one is defined, and usingAuth::user()->can('administrate')
works fine, even in the Middleware that defines the menu.
Since I have added the menu item viarouteIfCan
, I'm getting an error. It tells
Base table or view not found: 1146 Table 'db_b.permissions' doesn't exist (SQL: select
permissions
.*,model_has_permissions
.model_id
aspivot_model_id
,model_has_permissions
.permission_id
aspivot_permission_id
,model_has_permissions
.model_type
aspivot_model_type
frompermissions
inner joinmodel_has_permissions
onpermissions
.id
=model_has_permissions
.permission_id
wheremodel_has_permissions
.model_id
= 1 andmodel_has_permissions
.model_type
= App\User)
What is going wrong here? It should use theApp\User
model. Placing add()
at the point the framework throws the exception shows me the correct connection...
Please help.
Answer
Solution:
this mean tablepermissions
not exist on your database maybe you forgot to runphp artisan migrate
after install laravel-permission?
Answer
Solution:
A member of spatie helped to solve the problem:
Under the hood, routeIfCan calls app(Gate::class)->allows($ability, $ablityArguments). I assume Gate behaves slightly different than Auth::user() when it comes to multiple guards. I don't see much room in routeIfCan to add an additional $guard or $connection argument, so I suggest you use $menu->addIf(Auth::user()->can('administrate'), ...) instead.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: a non well formed numeric value encountered
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.