Laravel Import Large SQL File using Seeder Example

Admin   Laravel   153  2021-03-14 12:00:05

Hi Dev,

Now let's see example of how to import large sql file using seeder in laravel application. We will talk about laravel import large sql file using seeder example. you'll learn laravel seeder large sql file. you can understand a concept of laravel seed from sql file large file.

You can easy and simple way to import large file using seeder in laravel app. you'll learn laravel seeder large sql file. you can understand a concept of laravel seed from sql file large file.

Here I simply create seeder using bellow command So let's open terminal and run bellow command:

 

php artisan make:seeder ImportTableSeeder

now it's create ImportTableSeeder.php file on seeders folder. so let's update as bellow:

make sure you have one sql file call "data.sql" in public folder

database/seeders/ImportTableSeeder.php

 

<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;

class ImportTableSeeder extends Seeder

{

/**

* Run the database seeds.

*

* @return void

*/

public function run()

{

$sql = public_path('data.sql');

$db = [

'username' => env('DB_USERNAME'),

'password' => env('DB_PASSWORD'),

'host' => env('DB_HOST'),

'database' => env('DB_DATABASE')

];

exec("mysql --user={$db['username']} --password={$db['password']} --host={$db['host']} --database {$db['database']} < $sql");

\Log::info('SQL Import Done');

}

}

now you can easily run with bellow command:

 

php artisan db:seed --class=ImportTableSeeder

It will help you....