feat: 分離 AdminUserSeeder 並重構 DatabaseSeeder 支援單獨執行
This commit is contained in:
38
database/seeders/AdminUserSeeder.php
Normal file
38
database/seeders/AdminUserSeeder.php
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Database\Seeders;
|
||||||
|
|
||||||
|
use App\Models\User;
|
||||||
|
use Illuminate\Database\Seeder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 管理員帳號 Seeder
|
||||||
|
*
|
||||||
|
* 執行方式:php artisan db:seed --class=AdminUserSeeder
|
||||||
|
*/
|
||||||
|
class AdminUserSeeder extends Seeder
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the database seeds.
|
||||||
|
*/
|
||||||
|
public function run(): void
|
||||||
|
{
|
||||||
|
// 檢查是否已存在 admin 帳號,避免重複建立
|
||||||
|
$admin = User::where('username', 'admin')->first();
|
||||||
|
|
||||||
|
if ($admin) {
|
||||||
|
$this->command->info('Admin 帳號已存在,跳過建立。');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
User::create([
|
||||||
|
'username' => 'admin',
|
||||||
|
'name' => 'Admin',
|
||||||
|
'email' => 'admin@star-cloud.com',
|
||||||
|
'password' => bcrypt('password'),
|
||||||
|
'role' => 'admin',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->command->info('Admin 帳號建立成功!');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,16 +9,14 @@ class DatabaseSeeder extends Seeder
|
|||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Seed the application's database.
|
* Seed the application's database.
|
||||||
|
*
|
||||||
|
* 執行全部 Seeder:php artisan db:seed
|
||||||
|
* 執行單一 Seeder:php artisan db:seed --class=AdminUserSeeder
|
||||||
*/
|
*/
|
||||||
public function run(): void
|
public function run(): void
|
||||||
{
|
{
|
||||||
// 建立管理員帳號
|
$this->call([
|
||||||
\App\Models\User::factory()->create([
|
AdminUserSeeder::class,
|
||||||
'username' => 'admin',
|
|
||||||
'name' => 'Admin',
|
|
||||||
'email' => 'admin@star-cloud.com',
|
|
||||||
'password' => bcrypt('password'),
|
|
||||||
'role' => 'admin',
|
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user