Seed database using SQL file in Laravel

Created at 08-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

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Buy Me A Coffee

Don't forget to share this article! Help us spread the word by clicking the share button below.

We appreciate your support and are committed to providing you valuable and informative content.

We are thankful for your never ending support.