How to Run SQL File or query In Laravel Seeder?

Laravel 6 , Laravel , Laravel 7

Nicesnippets

51685

04-09-2020


How to Run SQL File or query In Laravel Seeder?

Hi Guys,

In this example,I will learn you how to run sql file or query in laravel seeder.you can easy run sql file or query in laravel seeder.

In this example,I will create sql folder in public and store to sql file.you can bellow this code in your example.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

It will help you....


Recommended Posts