Create a command line to make a user admin in Laravel 9

14 June
  • tutorial, programming, technical, tools

Being able to create your own command lines can be very useful is certain situations.

In this very precise situation, we are about to deploy on production an app with some Users. We have set a role to a user that is either “user” or “admin” and as it is set by default as “user”. (When a user is created it is automatically a normal user).

When you deploy it to production, you might run into the problem that no user are admin and your backend system might be inaccessible 😑.

An easy solution for this would be to create a command line where you can just enter the email of the user you want to make admin and poof!. Let’s tackle that together.

Let’s start from the beginning, and let’s add a role to our user model.

run this into your terminal: php artisan make:migration AddRoleToUsers

This command will generate our migration, we just need to change some bits. Mostly to make sure the role is set to “user” by default.

<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('institutions', function (Blueprint $table) {
            $table->string('status')->default('active'); 
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('institutions', function (Blueprint $table) {
            $table->dropColumn('status');
        });
    }
};

Don’t forget to run your migration with php artisan migrate

Here’s the bit you’re been scrolling for ! How to create a command line

Simply run php artisan make:command MakeAdmin in your terminal. Doing this will create a file called MakeAdmin.php into your app/console/command directory.

There is a few interesting things to note in this file :

the protected $signature is where you decide the command you would like to use in the console, and you can pass an argument to it. In this case, we are going to pass the email address of the user (you can use an other field if you would like). It will look like this :

protected $signature = 'make:admin {UserEmail}';

the next interesting line is the protected $description This will give a description to your command line and be accessible when using php artisan list.

Use something simple like this : protected $description = 'Make A User Admin';

Still in MakeAdmin.php, you will see a handle method. This is where you want to implement your behaviour.

Here we want to find the user that matches the email and change his role to “admin”

public function handle()
  {
      $user_email = $this->argument('UserEmail');
      User::where('email', $user_email)
      ->update([
          'role' => 'admin'
      ]);
      echo "Finished"; 
  }

Now go in your console and type php artisan admin:make [email protected]

The console will give you a “Finished” as a reward and you should be good to go !

🤯 So simple, right ?

BONUS : If you are using Nova as your backend go into app/Providers/NovaServiceProvider.php and change the access permission by using :

protected function gate()
    {
        Gate::define('viewNova', function ($user) {
            return $user->role == 'admin';
        });
    }

This will block the access to your nova backend to non-admin users.

Hope this was useful !

Photo by Ben Griffiths on Unsplash