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....
- How to Notifications With database Driver in Laravel 11?
- How to Send Email Via Notification in Laravel 11?
- How to Install and Configure Laravel 11 Debugbar?
- How to Like Dislike System in Laravel 11?
- How to Paginate a Model with Relationship in Laravel 11?
- Laravel 11 Upload Files to Amazon S3 Example
- Laravel 11 Send WhatsApp Messages Example
- Laravel 11 Rest API Authentication Using JWT Tutorial