LARAVEL

Basic setups


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:

1. Set Up Laravel Project

  1. Install Laravel via Composer:
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

 

2. Set Up Authentication

  1. Install Laravel Breeze (simple authentication scaffold):
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

 

3. Create Admin Middleware

  1. Generate Middleware:
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('/');
    }
}

 

4. Apply Middleware to Routes

  1. Register Middleware: Edit app/Http/Kernel.php:
protected $routeMiddleware = [
    // ...
    'admin' => \App\Http\Middleware\AdminMiddleware::class,
];
  1. Protect Admin Routes: Edit routes/web.php:
Route::group(['middleware' => ['auth', 'admin']], function () {
    Route::get('/admin', [AdminController::class, 'index'])->name('admin.dashboard');
});

 

5. Create Admin Controller

  1. Generate Controller:
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');
    }
}

 

6. Create Admin Views

  1. Create Dashboard View: Create 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>

 

7. Add Admin Column to Users Table

  1. Create Migration:
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');
    });
}
  1. Run Migration:
php artisan migrate

 

8. Update User Model

  1. Add Admin Attribute: Edit app/Models/User.php:
class User extends Authenticatable
{
    // ...
    protected $casts = [
        'email_verified_at' => 'datetime',
        'is_admin' => 'boolean',
    ];
}

 

9. Assign Admin Role to a User

  1. Create a Seeder (optional):
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();
        }
    }
}
  1. Run Seeder:
php artisan db:seed --class=AdminSeeder

 

10. Access Admin Panel

  1. Log in as Admin: Ensure your admin user (e.g., admin@codersmile.com) has is_admin set to true.
  2. Visit Admin Dashboard: Navigate to /admin in your browser.

LARAVEL