Simple user roles in Laravel app

It’s a common requirement for web-apps to have some kind of user roles defined. Although there are purpose-built packages for incorporating roles and permissions in Laravel app (e.g. Spatie Laravel-permission), however, in most of the cases the user roles requirement is as simple as categorizing users in different groups such as Admin, Manager, User etc. This kind of simple categorization can easily be achieved by amending built-in User model in Laravel. So, let’s incorporate user roles capability inside a Laravel 9 app.

Laravel 9.x requires a minimum PHP version of 8.0. This tutorial assumes that you are using Linux, MacOS or WSL on Windows and Node.js, Composer and PHP 8 along with required modules i.e bcmatch, sqlite, mbstring, xml, zip, gd, mcrypt are properly installed. Also make sure that SQLite is installed as we’ll be using SQLite to keep things simple and quick; you may use MySQL or PostgreSQL as you like, however make sure the corresponding PHP module is installed too.

Using Composer, enter following command in terminal to install Laravel 9;

composer create-project laravel/laravel app 9.*

Now go inside newly created app directory by executing;

cd app

Then, setup database and Laravel environment file. Here, I’m configuring SQLite database for simplicity purposes.

Being in parent directory (app), execute following command in order to create new SQLite database file;

touch database/database.sqlite

Then find .env config file in the parent directory, open it and amend DB_CONNECTION line as follows;

DB_CONNECTION = sqlite

You can remove DB_HOST, DB_PORT, DB_DATABASE, DB_USERNAME, DB_PASSWORD from .env when using SQLite database.

Now, enter following command to create update migration for users table:

php artisan make:migration update_users_table

Then insert following code inside newly generated migration file database/migrations/(date_stamp)_update_users_table.php:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
           $table->string('role')->nullable();
        });
    }

    public function down()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->dropColumn(['role']);
        });
    }
};

Now enter following command to run migrations:

php artisan migrate

Open app/Models/User.php and insert ‘role‘ field in protected $fillable array as given below;

    protected $fillable = [
        'name',
        'email',
        'password',
        'role',
    ];

All is set to create users with roles. So, let’s create a couple of users by invoking tinker;

php artisan tinker

Create first user by entering following command;

User::create(["name"=> "John Doe", "email"=>"john@gmail.com", "password"=>bcrypt("password"), "role"=>"admin"]);

Create second user by executing following command;

User::create(["name"=> "Jane Doe", "email"=>"jane@gmail.com", "password"=>bcrypt("password"), "role"=>"user"]);

You can check the inserted data by running following command in tinker;

User::all();

Now, type ‘exit’ and press Enter to leave tinker.

We have successfully created a couple of users with role attribute. Now, we can write logic based on roles of the users. In next tutorial we’ll use middleware to demonstrate this user role functionality.

As mentioned above for more powerful roles and permissions functionality, specialized packages like Spatie Laravel-permission are used; you may refer this article for utilizing Spatie Lavavel-permission in Laravel 9 app.

1 thought on “Simple user roles in Laravel app”

Leave a Comment