How to set Permission module on Laravel
How to setup Permission Module in Laravel
1) With help of packages
In Laravel, you can integrate the permission module by using a package such as Spatie's Laravel Permission package. Here are the steps to install and use the package:
- Install the package:
composer require spatie/laravel-permission
- Publish the migrations:
php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider" --tag="migrations"
- Run the migrations:
php artisan migrate
- Use the
HasRoles
trait in your User model:
use Spatie\Permission\Traits\HasRoles; class User extends Authenticatable { use HasRoles; }
- Define the permissions and assign them to roles:
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
$permission = Permission::create(['name' => 'edit articles']);
$role = Role::create(['name' => 'writer']);
$role->givePermissionTo($permission);
$user->assignRole('writer');
- Check if a user has a permission:
if ($user->can('edit articles')) {
// The user can edit articles...
}
This is a brief overview of integrating the permission module in Laravel using the Spatie's Laravel Permission package. You can find more information and advanced usage in the official documentation: https://docs.spatie.be/laravel-permission/v3/introduction/
_________________________________________________________________________________________________________
2) How to do it manually?
To implement permissions in Laravel manually, you can use Laravel's built-in authorization features and create your own custom solution. Here are the steps to do this:
1. Create a migration to add a roles
table that will store the roles for each user:php artisan make:migration create_roles_table
php artisan make:migration create_roles_table
2. Add the necessary fields to the roles
table, such as id
, name
, and description
:
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateRolesTable extends Migration
{
public function up()
{
Schema::create('roles', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('description')->nullable();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('roles');
}
}
3. Create a migration to add a permissions
table that will store the permissions for each role:
php artisan make:migration create_permissions_table
4. Add the necessary fields to the permissions
table, such as id
, name
, and description
:
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePermissionsTable extends Migration
{
public function up()
{
Schema::create('permissions', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('description')->nullable();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('permissions');
}
}
5. Create a migration to add a pivot table to connect roles and permissions:
php artisan make:migration create_role_permission_table
6. Add the necessary fields to the pivot table, such as role_id
and permission_id
:
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateRolePermissionTable extends Migration
{
public function up()
{
Schema::create('role_permission', function (Blueprint $table) {
$table->unsignedInteger('role_id');
$table->unsignedInteger('permission_id');
$table->timestamps();
$table->primary(['role_id', 'permission_id']);
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
$table->foreign('permission_id')->references('id')->on('permissions')->onDelete('cascade');
});
}
}
What's Your Reaction?






