# 1. 1 Create Roles && Permissions (ok)

### Cách 1:

<figure><img src="https://1957079826-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MCBeDUD-PK_RF-YgJRe%2Fuploads%2FNblW85GQVY5vX2ijqUF8%2Fimage.png?alt=media&#x26;token=b7d8b45e-a23e-45a2-bf12-27e172d10437" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1957079826-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MCBeDUD-PK_RF-YgJRe%2Fuploads%2F42E4wKfJamYlF8sRJtUN%2Fimage.png?alt=media&#x26;token=9d96ad5e-fcdd-45cf-8e8f-8aff268402ec" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1957079826-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MCBeDUD-PK_RF-YgJRe%2Fuploads%2FxeJfRo3XiNTP5K09Bclf%2Fimage.png?alt=media&#x26;token=6c6574fd-7bd4-4379-a3f9-591ce739cc03" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1957079826-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MCBeDUD-PK_RF-YgJRe%2Fuploads%2FkHaAKgdYoe36D1QyttGd%2Fimage.png?alt=media&#x26;token=9922fe98-3676-4563-8f71-1259d378b823" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1957079826-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MCBeDUD-PK_RF-YgJRe%2Fuploads%2FDK7FMueFlQfw4LHuRYCY%2Fimage.png?alt=media&#x26;token=06347a70-7832-4991-975a-8aadd08ae60a" alt=""><figcaption></figcaption></figure>

C:\xampp82\htdocs\testnet\database\seeders\DatabaseSeeder.php

```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([
      PermissionSeeder::class,
      UserRoleSeeder::class,
    ]);
  }
}

```

C:\xampp82\htdocs\testnet\database\seeders\PermissionSeeder.php

```php
<?php
namespace Database\Seeders;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Permission;
class PermissionSeeder extends Seeder
{
  /**
   * Run the database seeds.
   */
  public function run(): void
  {
    $permissions = [
      'browse bread 1',
      'read bread 1',
      'edit bread 1',
      'add bread 1',
      'delete bread 1',
    ];
    foreach ($permissions as $permission) {
      Permission::create(['name' => $permission]);
    }
  }
}

```

C:\xampp82\htdocs\testnet\database\seeders\UserRoleSeeder.php

```php
<?php
namespace Database\Seeders;
use App\Models\User;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Spatie\Permission\Models\Role;
use Illuminate\Database\Seeder;
use Faker\Factory as Faker;
use Illuminate\Support\Str;
use Spatie\Permission\Models\Permission;
class UserRoleSeeder extends Seeder
{
  /**
   * Run the database seeds.
   */
  public function run(): void
  {
    $numberOfUsers = 8;
    $faker = Faker::create();
    $admin = Role::create(['name' => 'admin']);
    $user = Role::create(['name' => 'user']);
    Role::create(['name' => 'guest']);
    $user = User::create([
      'name' => 'admin',
      'email' => 'admin@admin.com',
      'password' => bcrypt('password'),
      'menuroles' => 'admin,user'
    ]);
    $user->assignRole('admin');
    $user->assignRole('user');
    $permissions = Permission::pluck('id','id')->all();
    $admin->syncPermissions($permissions);
    for ($i = 0; $i < $numberOfUsers; $i++) {
      $user = User::create([
        'name' => $faker->name(),
        'email' => $faker->unique()->safeEmail(),
        'email_verified_at' => now(),
        'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
        'remember_token' => Str::random(10),
        'menuroles' => 'user'
      ]);
      $role = $user->assignRole('user');
    }
  }
}

```

C:\xampp82\htdocs\testnet\app\Models\User.php

```php
<?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\Sanctum\HasApiTokens;
use Spatie\Permission\Traits\HasRoles;
class User extends Authenticatable
{
  use HasApiTokens, HasFactory, Notifiable, HasRoles;
  /**
   * The attributes that are mass assignable.
   *
   * @var array<int, string>
   */
  protected $fillable = [
    'name',
    'email',
    'password',
    'menuroles'
  ];
  /**
   * The attributes that should be hidden for serialization.
   *
   * @var array<int, string>
   */
  protected $hidden = [
    'password',
    'remember_token',
  ];
  /**
   * The attributes that should be cast.
   *
   * @var array<string, string>
   */
  protected $casts = [
    'email_verified_at' => 'datetime',
  ];
}

```

C:\xampp82\htdocs\testnet\database\migrations\2014\_10\_12\_000000\_create\_users\_table.php

```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('users', function (Blueprint $table) {
      $table->id();
      $table->string('name');
      $table->string('email')->unique();
      $table->timestamp('email_verified_at')->nullable();
      $table->string('password');
      $table->rememberToken();
      $table->string('menuroles')->nullable();
      $table->timestamps();
    });
  }
  /**
   * Reverse the migrations.
   */
  public function down(): void
  {
    Schema::dropIfExists('users');
  }
};

```

### Cách 2:

<figure><img src="https://1957079826-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MCBeDUD-PK_RF-YgJRe%2Fuploads%2FhjY16YxCwUCECsuy4ORY%2Fimage.png?alt=media&#x26;token=ac3156d5-0172-42d7-b2f7-0f9da3793d0d" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1957079826-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MCBeDUD-PK_RF-YgJRe%2Fuploads%2Fb6xiFjDzccFNtCoJy4pw%2Fimage.png?alt=media&#x26;token=82d8a352-e962-4e51-899f-83e965924d8b" alt=""><figcaption></figcaption></figure>

C:\xampp81\htdocs\testeu\database\seeders\DatabaseSeeder.php

```php
<?php
namespace Database\Seeders;
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use App\Models\User;
use App\Models\Banner;
use App\Models\FanTypeNameEn;
use App\Models\BannerFanType;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
  /**
   * Seed the application's database.
   *
   * @return void
   */
  public function run()
  {
    User::factory(12)->create();
    $this->call([
      UserRoleSeeder::class,
      // MenusTableSeeder::class,
    ]);
  }
}

```

C:\xampp81\htdocs\testeu\database\seeders\UserRoleSeeder.php

```php
<?php
namespace Database\Seeders;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Role;
use Illuminate\Support\Str;
use App\Models\Users as User;
class UserRoleSeeder extends Seeder
{
  /**
   * Run the database seeds.
   *
   * @return void
   */
  public function run()
  {
    $adminRole = Role::create(['name' => 'admin']);
    $userRole = Role::create(['name' => 'user']);
    $guestRole = Role::create(['name' => 'guest']);
    /*  insert users   */
    $user = User::create([
      'name' => 'admin',
      'email' => 'admin@admin.com',
      'email_verified_at' => now(),
      'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
      'remember_token' => Str::random(10),
      'menuroles' => 'user,admin'
    ]);
  }
}

```

C:\xampp81\htdocs\testeu\app\Models\User.php

```php
<?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\Sanctum\HasApiTokens;
class User extends Authenticatable
{
  use HasApiTokens, HasFactory, Notifiable;
  /**
   * The attributes that are mass assignable.
   *
   * @var array<int, string>
   */
  protected $fillable = [
    'name',
    'email',
    'password',
    'menuroles'
  ];
  /**
   * The attributes that should be hidden for serialization.
   *
   * @var array<int, string>
   */
  protected $hidden = [
    'password',
    'remember_token',
  ];
  /**
   * The attributes that should be cast.
   *
   * @var array<string, string>
   */
  protected $casts = [
    'email_verified_at' => 'datetime',
  ];
}

```

C:\xampp81\htdocs\testeu\app\Models\Users.php

```php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Users extends Model
{
  use HasFactory, SoftDeletes;
  protected $fillable = [
    'name',
    'email',
    'password',
    'menuroles'
  ];
  protected $dates = [
    'deleted_at'
  ];
}

```

C:\xampp81\htdocs\testeu\database\migrations\2014\_10\_12\_000000\_create\_users\_table.php

```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.
   *
   * @return void
   */
  public function up()
  {
    Schema::create('users', function (Blueprint $table) {
      $table->id();
      $table->string('name');
      $table->string('email')->unique();
      $table->timestamp('email_verified_at')->nullable();
      $table->string('password');
      $table->string('menuroles');
      $table->rememberToken();
      $table->timestamps();
      $table->softDeletes();
    });
  }
  /**
   * Reverse the migrations.
   *
   * @return void
   */
  public function down()
  {
    Schema::dropIfExists('users');
  }
};

```
