Loading image

Blogs / Programming

Spatie's Role and Permission package in Laravel

Spatie's Role and Permission package in Laravel

  • Muhammad Abbas
  • 0 Comments
  • 277 View

To install Spatie's Role and Permission package in Laravel, follow the steps below:

Step 1: Install the package via Composer

Run the following command to install the Spatie Role and Permission package:

composer require spatie/laravel-permission

Step 2: Publish the configuration file

After installation, you need to publish the package's configuration file by running the following Artisan command:

 

php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider"

This will create a config/permission.php file where you can customize the package's settings.

 

Step 3: Run the migrations

 

The package requires a set of database tables to store roles and permissions. Run the migrations to create these tables:

 

 

php artisan migrate

 

 

 

ADD In User Model

use Spatie\Permission\Traits\HasRoles;
class User extends Authenticatable{
     use HasRoles
 }

 

After installation of Spatie

     * Run the database seeds.

    public function run(): void

    {

        $user = User::create([

            'name' => 'Adminn',

            'email' => '[email protected]',

            'email_verified_at' => now(),

            'password' => bcrypt('password'),  // Make sure to hash the password

        ]);

        $user->assignRole(['writer','admin']);

    }

}

 

class RoleSeeder extends Seeder

{
    /**
     * Run the database seeds.
     */

    public function run(): void

    {

        Role::create(['name' => 'admin']);

        Role::create(['name' => 'user']);

        Role::create(['name' => 'writer']);

    }

}

class DatabaseSeeder extends Seeder

{

    /**
     * Seed the application's database.
     */

    public function run(): void

    {
            $this->call(RoleSeeder::class);

            $this->call(AdminSeeder::class);

    }

}

 

 

After creating AdminSeeder and RoleSeeder run this command

php artisan migrate:fresh –seed

After that we create

PermissionController

RoleController

Check for Roles and Permissions in Blade

 

 

In your Blade templates, you can use the @role@can, and @hasrole directives provided by the Spatie package to define what content certain users are allowed to see or access.

Here are some examples:

Using @role directive:

@role('admin')

    <p>This is visible to users with the admin role.</p>

@endrole

Using @can directive:

@can('edit posts')

    <p>This is visible to users who have the 'edit posts' permission.</p>

@endcan

Using @hasrole directive:

@hasrole('writer')

    <p>This is visible to users with the writer role.</p>

@endhasrole

 

Combining roles and permissions:

 

@role('admin')

    <p>Only visible to admins.</p>

@elsecan('edit posts')

    <p>Visible to users who can edit posts.</p>

@endrole


You can also check multiple roles and permissions with @hasanyrole or @hasallroles:

@hasanyrole('admin|writer')

    <p>Visible to users with either the admin or writer role.</p>

@endhasanyrole

 

@hasallroles('admin|writer')

    <p>Visible only to users who have both admin and writer roles.</p>

@endhasallroles

 

 

  • Programming
Muhammad Abbas Author

Muhammad Abbas

Hello! I’m Muhammad Abbas, a passionate and driven junior web developer with a strong foundation in back-end and front-end technologies and a keen interest in creating dynamic, user-centric web experiences. I honed my skills in JavaScript, HTML, CSS, and developed a solid understanding of web development principles.

0 Comments

Post Comment

Recent Blogs

Recent posts form our Blog

How to Create Custom Route File in Laravel 11

How to Create Custom Route File in Laravel 11

showkat ali
/
Programming

Read More
Step-by-Step Guide: Unzipping, Extracting, and Saving ZIP Files to a Database in Laravel

Step-by-Step Guide: Unzipping, Extracting, and Saving ZIP Files to a Database in Laravel

showkat ali
/
Programming

Read More
Using a Mind Map to Brainstorm Ideas and Finding Solutions to Complex Problems

Using a Mind Map to Brainstorm Ideas and Finding Solutions to Complex Problems

Nasir Hussain
/
English

Read More
Five Steps Sample Lesson Plan for English Grade 5th, 6th, 7th 8th, 9th, and 10th

Five Steps Sample Lesson Plan for English Grade 5th, 6th, 7th 8th, 9th, and 10th

Nasir Hussain
/
English

Read More
Laravel 11.24 Released: New Features Unveiled

Laravel 11.24 Released: New Features Unveiled

showkat ali
/
Programming

Read More
A Step-by-Step Guide:How to integrate stripe payment into laravel 10

A Step-by-Step Guide:How to integrate stripe payment into laravel 10

showkat ali
/
Programming

Read More