C:\xampp82\htdocs\testcom\app\Models\User.php
<?php
namespace App\Models;
use Spatie\Permission\Traits\HasRoles;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable, HasRoles;
}
C:\xampp82\htdocs\testcom\database\migrations\2023_04_04_023335_create_roles_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('roles', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('guard_name');
$table->timestamps();
$table->unique(['name','guard_name']);
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('roles');
}
};
C:\xampp82\htdocs\testcom\database\migrations\2023_04_04_024109_create_permissions_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('permissions', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('guard_name');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('permissions');
}
};
C:\xampp82\htdocs\testcom\database\migrations\2023_04_04_024553_create_table_role_has_permissions_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('role_has_permissions', function (Blueprint $table) {
$table->unsignedBigInteger('role_id');
$table->unsignedBigInteger('permission_id');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('role_has_permissions');
}
};
C:\xampp82\htdocs\testcom\database\migrations\2023_04_04_024810_create_table_model_has_roles_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('model_has_roles', function (Blueprint $table) {
$table->unsignedBigInteger('model_id');
$table->string('model_type');
$table->unsignedBigInteger('role_id');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('model_has_roles');
}
};
C:\xampp82\htdocs\testcom\database\migrations\2023_04_04_025302_create_table_model_has_permissions_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('model_has_permissions', function (Blueprint $table) {
$table->unsignedBigInteger('model_id');
$table->string('model_type');
$table->unsignedBigInteger('permission_id');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('model_has_permissions');
}
};
C:\xampp82\htdocs\testcom\database\seeders\CreateAdminUserSeeder.php
<?php
namespace Database\Seeders;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use App\Models\User;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
class CreateAdminUserSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
$user = User::create([
'name' => 'lionel',
'email' => 'phamngoctuong1805@gmail.com',
'password' => bcrypt('12345678')
]);
$role = Role::create([
'name' => 'Administrator']
);
$permission = Permission::pluck('id','id')->all(); // => array ( 1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5, 6 => 6, 7 => 7, 8 => 8, 9 => 9, 10 => 10, 11 => 11, 12 => 12, )
// $permission = Permission::pluck('id','name')->all(); => array ( 'role-list' => 1, 'role-create' => 2, 'role-edit' => 3, 'role-delete' => 4, 'product-list' => 5, 'product-create' => 6, 'product-edit' => 7, 'product-delete' => 8, 'user-list' => 9, 'user-create' => 10, 'user-edit' => 11, 'user-delete' => 12, )
$role->syncPermissions($permission); // Create Table role_has_permissions
$user->assignRole([$role->id]); // Register Table model_has_roles use use Spatie\Permission\Traits\HasRoles; in Models\User.php
}
}
C:\xampp82\htdocs\testcom\database\seeders\PermissionTableSeeder.php
<?php
namespace Database\Seeders;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Permission;
class PermissionTableSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
$permissions = [
'role-list',
'role-create',
'role-edit',
'role-delete',
'product-list',
'product-create',
'product-edit',
'product-delete',
'user-list',
'user-create',
'user-edit',
'user-delete',
];
foreach ($permissions as $permission) {
Permission::create(['name' => $permission]);
}
}
}
C:\xampp82\htdocs\testcom\database\seeders\DatabaseSeeder.php
<?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([
PermissionTableSeeder::class,
CreateAdminUserSeeder::class,
]);
}
}