After "php artisan migrate:fresh" the admin is lost
How to prevent the admin being lost after migration from scratch?
Solution:Jump to solution
Add inside run method of the file
"database/seeders/DatabaseSeeder.php" this code:
public function run()
{
$this->call(AdminUserSeeder::class);...
4 Replies
You need to use this command: "php artisan migrate:fresh --seed". At the end of the database reconstruction, the seeder will create a new admin. But before doing this, make sure you have a seeder that creates the "admin" user.
php artisan make:seeder AdminUserSeeder
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
use App\Models\User;
class AdminUserSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
// Crea un utente admin
User::updateOrCreate(
['email' => '[email protected]'],
[
'name' => 'Admin',
'email' => '[email protected]',
'password' => Hash::make('admin password'),
]
);
$this->command->info('Admin user created successfully!');
}
}
Ensure that in the model the fillable fields are configured
protected $fillable = [
'name',
'email',
'password',
];
Solution
Add inside run method of the file
"database/seeders/DatabaseSeeder.php" this code:
public function run()
{
$this->call(AdminUserSeeder::class);
}
what about adding it in user migration?
// Insert the initial user
DB::table('users')->insert([
'name' => 'John Smith',
'email' => '[email protected]',
'password' => Hash::make('password'),
'role' => 'admin',
'created_at' => now(),
'updated_at' => now(),
'email_verified_at' => now(),
]);
You can just create it again or use a seeder
But if you are using something like Filament Shield which requires more setup for the admin and permissions you may just create a custom command that runs the
migrate:fresh
and the other needed commands and queries