Laravel-permission: Table model_has_permissions purpose?

Created on 18 Feb 2019  路  2Comments  路  Source: spatie/laravel-permission

I have a problem figuring it out, how do I use that table? with what commands? Is it possible to add permission to model?

support

Most helpful comment

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.

All 2 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tripex picture tripex  路  3Comments

bbdangar picture bbdangar  路  4Comments

MichalKrakow picture MichalKrakow  路  4Comments

neoreids picture neoreids  路  3Comments

dylangeorgeharbour picture dylangeorgeharbour  路  3Comments