Site Admin
Site Admin Founder of MeaningArticles
65 Views

Laravel Run SQL File or query In Seeder

Hello Dev.

In this example, i can learn you how to run sql file or query in laravel seeder. you may easily run sql file or query in laravel seeder.

In this example, i will create sql folder in public and store to sql file. you may bellow this code to your instance. so first run bellow command for create "SqlFileSeeder" seeder.


Example:

First create a seeder file by artisan command.
Create Seeder:

php artisan make:seeder SqlFileSeeder

After run above command successfully, you will be found new created file database/seeds/SqlFileSeeder.php file and some sample data like as bellow...
database/seeds/SqlFileSeeder.php

<?php

use Illuminate\Database\Seeder;

class SqlFileSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {

        $path = public_path('sql/File.sql');
        $sql = file_get_contents($path);
        DB::unprepared($sql);
    }

}

Now we are ready to run above seeder using bellow command...
Run Seeder:

php artisan db:seed --class=SqlFileSeeder

i'm hoping it assist you to, thanks for visit my article if you like my article then proportion together with your friend and social platform.