Seed database using SQL file in Laravel

Updated at 07-Jan-2022, By samar

Seed database using SQL file in Laravel

Through many examples, we will learn how to resolve the "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.
  • 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.
    

Back to code snippet queries related laravel