Use Policies only for companies Panel, not other
Friends, I have a problem, I am working on a tenancy app with integration of roles and permissions, for the companies panel everything works ok, the problem is that when entering the administration panel the app blocks and does not allow me to view the information, this happens because I am using within the administration panel, the same Model as in Companies, I am referring to the User model, I was looking for an exclusive policy that could be used for said panels, and in chatgpt it suggests this example, but it still doesn't work for me. works.
This is my Policies for Companies Panel
2 Replies
this is my Policies only for AdminPanel
and this is my panel
this return me an error
TypeError
PHP 8.2.4
10.33.0
App\Policies\UserPolicy::viewAny(): Argument #1 ($user) must be of type App\Models\User, App\Models\Admin given, called in C:\Users\mikey\Documents\myfit-multitenancy\vendor\laravel\framework\src\Illuminate\Auth\Access\Gate.php on line 811
App\Policies\UserPolicy::viewAny(): Argument #1 ($user) must be of type App\Models\User, App\Models\Admin given
public function viewAny(User $user): bool
The error message shows that it's looking for an Admin
model, not a User
model.
And, if you look at your code, in your Policy's typehint that's because you're expecting a User
model, not Admin
.
If your models are only Admin
then you will need to change that typehint accordingly.