top of page

Posts

How to Set Up Laravel Admin


In this article, I will talk about setting up Laravel Admin.


What is Laravel Admin?

Laravel Admin is a tool for administrators developed using the Laravel framework. 

It allows for the easy creation of application management dashboards and control panels. With Laravel Admin, you can manage various data and resources.


Environment

Docker: 23.0.1

Laravel: 10.37.1

PHP: 8.2

MySQL: 8.0

Nginx: 1.23.3


Setup Steps

Display the Login Screen

First, execute the following command to install Laravel Admin

composer require encore/laravel-admin

Next, add the installed Laravel Admin files to your Laravel project by running this command

php artisan vendor:publish --provider="Encore\Admin\AdminServiceProvider"

Then, create the standard tables for Laravel Admin with this command

php artisan admin:install

If the above command is successfully executed, the following message is displayed and the creation of standard tables is complete. 

root@d816ac03c504:/var/www/html# php artisan admin:install
 
INFO Preparing database.
 
Creating migration table .............................................................................................................. 126ms DONE
 
INFO Running migrations.
 
2014_10_12_000000_create_users_table .................................................................................................. 133ms DONE
2014_10_12_100000_create_password_reset_tokens_table .................................................................................. 190ms DONE
2016_01_04_173148_create_admin_tables ................................................................................................. 976ms DONE
2019_08_19_000000_create_failed_jobs_table ............................................................................................ 101ms DONE
2019_12_14_000001_create_personal_access_tokens_table ................................................................................. 147ms DONE
 
INFO Seeding database.
 
Admin directory was created: /app/Admin
HomeController file was created: /app/Admin/Controllers/HomeController.php
AuthController file was created: /app/Admin/Controllers/AuthController.php
ExampleController file was created: /app/Admin/Controllers/ExampleController.php
Bootstrap file was created: /app/Admin/bootstrap.php
Routes file was created: /app/Admin/routes.php

Now, try accessing the Laravel-Admin login screen. If everything goes well, you will see the login page.


Creating an Admin User

Although the login screen is ready, the crucial login user has not been created yet. Next, we will create an administrator user for login. 

Start by creating a suitable seeder. 

In this case, execute the following command to create a UserSeeder.

php artisan make:seeder UserSeeder

Next, to use the seeder under the vendor directory, open the /vendor/encore/laravel-admin/src/Auth/Database/AdminTablesSeeder.php file and copy the contents inside the public function run(){}

Replace the contents of public function run(){} in UserSeeder with the copied content. 

The replaced content is as follows.


<?php
 
namespace Database\Seeders;
 
use Encore\Admin\Auth\Database\Administrator;
use Encore\Admin\Auth\Database\Menu;
use Encore\Admin\Auth\Database\Permission;
use Encore\Admin\Auth\Database\Role;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
 
class UserSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run()
    {
        // create a user.
        Administrator::truncate();
        Administrator::create([
            'username' => 'admin',
            'password' => Hash::make('admin'),
            'name'     => 'Administrator',
        ]);
 
        // create a role.
        Role::truncate();
        Role::create([
            'name' => 'Administrator',
            'slug' => 'administrator',
        ]);
 
        // add role to user.
        Administrator::first()->roles()->save(Role::first());
 
        //create a permission
        Permission::truncate();
        Permission::insert([
            [
                'name'        => 'All permission',
                'slug'        => '*',
                'http_method' => '',
                'http_path'   => '*',
            ],
            [
                'name'        => 'Dashboard',
                'slug'        => 'dashboard',
                'http_method' => 'GET',
                'http_path'   => '/',
            ],
            [
                'name'        => 'Login',
                'slug'        => 'auth.login',
                'http_method' => '',
                'http_path'   => "/auth/login\r\n/auth/logout",
            ],
            [
                'name'        => 'User setting',
                'slug'        => 'auth.setting',
                'http_method' => 'GET,PUT',
                'http_path'   => '/auth/setting',
            ],
            [
                'name'        => 'Auth management',
                'slug'        => 'auth.management',
                'http_method' => '',
                'http_path'   => "/auth/roles\r\n/auth/permissions\r\n/auth/menu\r\n/auth/logs",
            ],
        ]);
 
        Role::first()->permissions()->save(Permission::first());
 
        // add default menus.
        Menu::truncate();
        Menu::insert([
            [
                'parent_id' => 0,
                'order'     => 1,
                'title'     => 'Dashboard',
                'icon'      => 'fa-bar-chart',
                'uri'       => '/',
            ],
            [
                'parent_id' => 0,
                'order'     => 2,
                'title'     => 'Admin',
                'icon'      => 'fa-tasks',
                'uri'       => '',
            ],
            [
                'parent_id' => 2,
                'order'     => 3,
                'title'     => 'Users',
                'icon'      => 'fa-users',
                'uri'       => 'auth/users',
            ],
            [
                'parent_id' => 2,
                'order'     => 4,
                'title'     => 'Roles',
                'icon'      => 'fa-user',
                'uri'       => 'auth/roles',
            ],
            [
                'parent_id' => 2,
                'order'     => 5,
                'title'     => 'Permission',
                'icon'      => 'fa-ban',
                'uri'       => 'auth/permissions',
            ],
            [
                'parent_id' => 2,
                'order'     => 6,
                'title'     => 'Menu',
                'icon'      => 'fa-bars',
                'uri'       => 'auth/menu',
            ],
            [
                'parent_id' => 2,
                'order'     => 7,
                'title'     => 'Operation log',
                'icon'      => 'fa-history',
                'uri'       => 'auth/logs',
            ],
        ]);
 
        // add role to menu.
        Menu::find(2)->roles()->save(Role::first());
    }
}

After creating the UserSeeder, include it in the DatabaseSeeder.

<?php
 
namespace Database\Seeders;
 
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
 
class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     */
    public function run(): void
    {
        $this->call([
            UserSeeder::class,
        ]);
    }
}

Once you have completed the above, all that's left is to run the seeder.

	php artisan db:seed

After the seeder completes, return to the login screen and enter the username and password set by the seeder. 

In this example, both the username and password are set to 'admin'. Please change these when actually using it. 

After logging in with both set to 'admin', you will be redirected to the following screen.


Now, you can log in successfully. The setup of Laravel Admin is completed.


Conclusion

Setting up Laravel Admin is very simple, and you can quickly build an admin panel. Also, if you create tables with Laravel Admin, you can operate these tables via a GUI, so please give it a try.



This blog post is translated from a blog post written by Matsuki on our Japanese website Beyond Co..

bottom of page