หากคุณต้องการระบบ Role / Permission (ACL) , RBAC ที่มาพร้อมกับ หน้าจอจัดการ (UI) และฟีเจอร์ความปลอดภัยแบบจัดเต็มโดยไม่ต้องเขียนเองเยอะ “การลง Jetstream (Livewire) , Spatie Laravel-Permission คือทางเลือกที่ดีมาก” ครับ
- ติดตั้ง Laravel 12 (Clean Install)
เปิด Terminal และไปที่ Path ที่คุณกำหนด เพื่อสร้างโปรเจกต์ใหม่cd /Users/Shared/Gits/phunsanit/prototypes-laravel-spatie
composer create-project laravel/laravel Sourcecode
cd Sourcecode - ตั้งค่า Database (.env) เช่น
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE={DB_DATABASE} DB_USERNAME={DB_USERNAME} DB_PASSWORD={DB_PASSWORD} - ติดตั้ง Laravel Jetstream (Livewire Stack)
Jetstream จะจัดการระบบ Login, Profile, และ Security ทั้งหมดให้เราcomposer require laravel/jetstream
php artisan jetstream:install livewire - ติดตั้ง Spatie Laravel-Permission
ติดตั้งตัวจัดการ ACL เพื่อเสริมเขี้ยวเล็บให้ Jetstreamcomposer require spatie/laravel-permission
php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider" - รัน Migration พร้อมกัน
ขั้นตอนนี้จะสร้างตารางของทั้ง Jetstream และ Spatie ลงใน dtabasephp artisan migrate - ตั้งค่า Model User
เปิดไฟล์ app/Models/User.php และเพิ่มHasRolestrait<?php namespace App\Models; // use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Laravel\Fortify\TwoFactorAuthenticatable; use Laravel\Jetstream\HasProfilePhoto; use Laravel\Sanctum\HasApiTokens; use Spatie\Permission\Traits\HasRoles; // Add this line to include the HasRoles trait class User extends Authenticatable { use HasApiTokens; /** @use HasFactory<\Database\Factories\UserFactory> */ use HasFactory; use HasProfilePhoto; use Notifiable; use TwoFactorAuthenticatable; use HasRoles; // Add this line to use the HasRoles trait /** * The attributes that are mass assignable. * * @var array<int, string> */ protected $fillable = [ 'name', 'email', 'password', ]; /** * The attributes that should be hidden for serialization. * * @var array<int, string> */ protected $hidden = [ 'password', 'remember_token', 'two_factor_recovery_codes', 'two_factor_secret', ]; /** * The accessors to append to the model's array form. * * @var array<int, string> */ protected $appends = [ 'profile_photo_url', ]; /** * Get the attributes that should be cast. * * @return array<string, string> */ protected function casts () : array { return [ 'email_verified_at' => 'datetime', 'password' => 'hashed', ]; } } - สร้างระบบ Role เริ่มต้น (Seeder)
สร้างไฟล์ Seeder:php artisan make:seeder RoleSeederแก้ไขไฟล์database/seeders/RoleSeeder.phpเพื่อสร้าง Admin คนแรก<?php namespace Database\Seeders; use Illuminate\Database\Console\Seeds\WithoutModelEvents; use Illuminate\Database\Seeder; class RoleSeeder extends Seeder { /** * Run the database seeds. */ public function run () : void { // clear cached roles and permissions app () [\Spatie\Permission\PermissionRegistrar::class]->forgetCachedPermissions () ; // create role 'admin' $adminRole = \Spatie\Permission\Models\Role::firstOrCreate (['name' => 'admin']) ; // create fist user and assign role 'admin' $user = \App\Models\User::updateOrCreate (['email' => '[email protected]'], [ 'name' => 'Phunsanit Admin', 'password' => bcrypt ('12345678') , ]) ; // assign role 'admin' to the user $user->assignRole ($adminRole) ; } } - รัน Seeder
php artisan db:seed --class=RoleSeeder - คอมไพล์ Assets (CSS / JS)
เพื่อให้หน้าตา Jetstream แสดงผลถูกต้อง (ต้องใช้ Node.js ในเครื่อง)npm installnpm run build
วิธีการเข้าใช้งาน (Login)
- รัน Server:
php artisan serve - เปิด Browser ไปที่:
http://127.0.0.1:8000/login - เข้าใช้งานด้วย
- Email:
[email protected] - Password:
12345678
- Email:
สรุปผลลัพธ์ที่คุณจะได้
- Jetstream: จัดการหน้า Login, หน้า Profile, และระบบ Two-Factor Auth
- Spatie: จัดการสิทธิ์ว่าใครคือ Admin หรือ User ทั่วไป
อ่านเพิ่มเติม