Created
November 13, 2024 12:48
-
-
Save bradtraversy/30efc04f3dac77cd8fecec5a6229fd97 to your computer and use it in GitHub Desktop.
Laravel seeder for MySQL/MariaDB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// TRY THIS FIRST. DISABLE AND RE_ENABLE THE CHECK | |
namespace Database\Seeders; | |
use Illuminate\Database\Seeder; | |
use Illuminate\Support\Facades\DB; | |
class DatabaseSeeder extends Seeder | |
{ | |
/** | |
* Seed the application's database. | |
*/ | |
public function run(): void | |
{ | |
// Disable foreign key checks | |
DB::statement('SET FOREIGN_KEY_CHECKS=0;'); | |
// Truncate tables | |
DB::table('job_user_bookmarks')->truncate(); | |
DB::table('applicants')->truncate(); | |
DB::table('job_listings')->truncate(); | |
DB::table('users')->truncate(); | |
// Re-enable foreign key checks | |
DB::statement('SET FOREIGN_KEY_CHECKS=1;'); | |
// Call seeders | |
$this->call(TestUserSeeder::class); | |
$this->call(RandomUserSeeder::class); | |
$this->call(JobSeeder::class); | |
$this->call(BookmarkSeeder::class); | |
} | |
} | |
//////////////////////////////////////////////////////////////////////////////////////////// | |
// THIS MAY WORK AS WELL> USING DELETE INSTEAD OF TRUNCATE | |
namespace Database\Seeders; | |
use Illuminate\Database\Seeder; | |
use Illuminate\Support\Facades\DB; | |
class DatabaseSeeder extends Seeder | |
{ | |
/** | |
* Seed the application's database. | |
*/ | |
public function run(): void | |
{ | |
// Clear tables without truncating | |
DB::table('job_user_bookmarks')->delete(); | |
DB::table('applicants')->delete(); | |
DB::table('job_listings')->delete(); | |
DB::table('users')->delete(); | |
// Call seeders | |
$this->call(TestUserSeeder::class); | |
$this->call(RandomUserSeeder::class); | |
$this->call(JobSeeder::class); | |
$this->call(BookmarkSeeder::class); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment