You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

57 lines
1.6 KiB

<?php
namespace Database\Seeders;
use App\Models\Role;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use App\Models\User;
use Spatie\Permission\Models\Permission;
class CreateAdminUserSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
try {
$user = User::create([
'account' => 'admin',
'name' => 'admin',
'email' => '[email protected]',
'password' => bcrypt('!@#sh22463458')
]);
} catch (\Throwable $th) {
try {
$user = User::where('account', 'admin')->first();
} catch (\Throwable $th) {
//throw $th;
}
//throw $th;
}
try {
$role = Role::create(['name' => 'Admin','guard_name' => '*','display_name' => '開發者']);
$manager = Role::create(['name' => 'Manager','guard_name' => '*','display_name' => '系統管理員']);
} catch (\Throwable $th) {
try {
$role = Role::where('name', 'Admin')->first();
} catch (\Throwable $th) {
//throw $th;
}
//throw $th;
}
try {
$permissions = Permission::pluck('id','id')->all();
$role->syncPermissions($permissions);
$manager->syncPermissions($permissions);
} catch (\Throwable $th) {
// throw $th;
}
try {
$user->assignRole([$role->id]);
} catch (\Throwable $th) {
throw $th;
}
}
}