Seed database using SQL file in Laravel

You can seed database using SQL file in Laravel. You have to place the database in project root directory and call the DB::unprepared() method to seed the database. This is the simple way to seed a database in Laravel with a .sql dump file.

Answers 1
  • Seed database using SQL file in Laravel

    //1. Place database file (laravel8.sql in my case) in root directory of your project.
    //2. Use the below code snippet in routes\web.php and visit /seed-database url to seed database.
    Route::get('/seed-database', function(){
        DB::unprepared(
            file_get_contents(base_path() . './laravel8.sql')
        );
    });
    
    //Note : You have to create the database and configure in application's environment variables.
    
    0
  • Back to code snippet queries related laravel