Setting up an admin panel in a Laravel application involves several steps, from setting up authentication to building the interface for managing your application's data. Here’s a step-by-step guide:
composer create-project --prefer-dist laravel/laravel admin-panel //for latest version
composer create-project --prefer-dist laravel/laravel:^9.0 admin-panel //for specific version
cd admin-panel
composer require laravel/breeze --dev
php artisan breeze:install
choose
any one according to you requirement in my case i have choose 0 for blade
only and with all default setting
npm install && npm run dev
php artisan migrate
php artisan make:middleware AdminMiddleware
Update Middleware: Edit app/Http/Middleware/AdminMiddleware.php
:
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class AdminMiddleware
{
public function handle($request, Closure $next)
{
if (Auth::check() && Auth::user()->is_admin) {
return $next($request);
}
return redirect('/');
}
}
app/Http/Kernel.php
:protected $routeMiddleware = [
// ...
'admin' => \App\Http\Middleware\AdminMiddleware::class,
];
routes/web.php
:Route::group(['middleware' => ['auth', 'admin']], function () {
Route::get('/admin', [AdminController::class, 'index'])->name('admin.dashboard');
});
php artisan make:controller AdminController
Add Dashboard Method: Edit app/Http/Controllers/AdminController.php
:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class AdminController extends Controller
{
public function index()
{
return view('admin.dashboard');
}
}
resources/views/admin/dashboard.blade.php
:<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Admin Dashboard</title>
</head>
<body>
<h1>Welcome to Admin Dashboard</h1>
</body>
</html>
php artisan make:migration add_is_admin_to_users_table --table=users
Update Migration: Edit the new migration file:
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->boolean('is_admin')->default(0);
});
}
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('is_admin');
});
}
php artisan migrate
app/Models/User.php
:class User extends Authenticatable
{
// ...
protected $casts = [
'email_verified_at' => 'datetime',
'is_admin' => 'boolean',
];
}
php artisan make:seeder AdminSeeder
Update Seeder: Edit database/seeders/AdminSeeder.php
:
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use App\Models\User;
class AdminSeeder extends Seeder
{
public function run()
{
$user = User::where('email', 'admin@codersmile.com')->first();
if ($user) {
$user->is_admin = true;
$user->save();
}
}
}
php artisan db:seed --class=AdminSeeder
codersmile
.com) has is_admin
set to true
./admin
in your browser.