Laravel Backpack Authentification | Create a CRUD admin panel for users

By default after installed Backpack, all users are considered admins.

If that’s not what you want in your application (you have both users and admins), please:

  • Change app/Http/Middleware/CheckIfAdmin.php, particularly checkIfUserIsAdmin($user), to make sure you only allow admins to access the admin panel;
  • Change app/Providers/RouteServiceProvider::HOME, which will send logged in (but not admin) users to /home, to something that works for your app;

Authentication

Backpack uses Laravel’s default App\User model.

This is part 3 of the series seo site manager in Laravel.

Create a CRUD administration panel for users

To create a CRUD administration panel for users, run

php artisan backpack:crud user

This command will create the “users” table and password_resets table.

Now will see the user area in the admin sidebar.
baclpack user area

or create your custom user table

1. Create the migration

php artisan make:migration create_users_table --table=users

2. Add add the columns you need for the “users” table

Below are the columns I added.

    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->string('image', 255)->nullable();     
            $table->integer('is_admin')->default(false);                      
            $table->timestamps();    
        });
    }

3. Run the migration

Because the user table already exists in my database, after I added the new fields (image, is_admin) I will run the php artisan migrate:fresh command.

The migrate:fresh command will drop all tables from the database and then execute the migrate command: php artisan migrate.

What is rememberToken() in the “users” table?

Before getting started, make sure that your users (or equivalent) table contains a nullable, string remember_token column of 100 characters. This column will be used to store a token for “remember me” sessions being maintained by your application. This can be done by using $table->rememberToken(); in a migration.

Install Laravel-Backpack PermissionManager

If You need to have both users and admin, Install Laravel-Backpack PermissionManager package. It allows admins to easily add/edit/remove users, roles and permissions, using Laravel Backpack.
Admin interface for managing users, roles, permissions, using Backpack CRUD.

This package is just a user interface for spatie/laravel-permission. It will install it, and let you use its API in code.


Leave a Comment

WebPedia.net