I have a problem figuring it out, how do I use that table? with what commands? Is it possible to add permission to model?
The model_has_permissions table is Laravel's pivot table for the morphToMany relationship between permissions and your various User models.
https://github.com/spatie/laravel-permission/blob/982f280618b5be70887f0c094aec75c832f1328d/src/Models/Permission.php#L61-L73
Since you can associate multiple User models with their own permissions, this needs to be tracked somewhere. That table stores those relationships.
Same with model_has_roles table.
You should never directly access that table. Let Eloquent do that, by using the commands this package provides. They're all documented in the README.
For example:
$user = User::where('email_address', '[email protected]')->first(); // uses the \App\User model
$user->givePermissionTo('edit-articles'); // stores the relationship
The above would store the App\User model_type and the $user->id model_id and the permission_id associated with the permissions table record id for edit-articles into the model_has_permissions table.
Again, you never access that table directly with any SQL queries, whether for reading or for writing. Always let Eloquent do it, using the functionality provided.
You can learn more about MorphToMany relationships in the Laravel docs and numerous blog posts online.
Awesome, thank you a lot.
Most helpful comment
The
model_has_permissionstable is Laravel's pivot table for themorphToManyrelationship between permissions and your various User models.https://github.com/spatie/laravel-permission/blob/982f280618b5be70887f0c094aec75c832f1328d/src/Models/Permission.php#L61-L73
Since you can associate multiple User models with their own permissions, this needs to be tracked somewhere. That table stores those relationships.
Same with
model_has_rolestable.You should never directly access that table. Let Eloquent do that, by using the commands this package provides. They're all documented in the README.
For example:
The above would store the
App\Usermodel_type and the$user->idmodel_id and thepermission_idassociated with thepermissionstable recordidforedit-articlesinto themodel_has_permissionstable.Again, you never access that table directly with any SQL queries, whether for reading or for writing. Always let Eloquent do it, using the functionality provided.
You can learn more about MorphToMany relationships in the Laravel docs and numerous blog posts online.