Laravel Import Large SQL File using Seeder Example
Hello Dev,
This example is focused on laravel import large sql file. if you want to see example of laravel import data from sql file then you are a right place. you'll learn laravel seeder large sql file. you can understand a concept of laravel seed from sql file large file.
you can easily import large sql file using seeder in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 version.
If you need to import directly sql file into database then how you will do? and if it's large file then how you can do it. i will give you simple example how to import large sql file using laravel seeder.
so let's simply create seeder with following command and write code as bellow:
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
now it will works for you.
i hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel 8 Database Seeder Tutorial Example
- Laravel Eloquent whereBetween() Query Example
- Laravel Migration - How to Add New Column in Existing Table ?
- How to Change Table Name using Laravel Migration?
- How to Remove Column from Table in Laravel Migration?
- How to Change Column Name and Data Type in Laravel Migration?
- How to Create Table using Migration in Laravel?
- How to create database seeder in Laravel 5.7?
- Laravel One to Many Eloquent Relationship Tutorial
- How to Create Database Seeder in Laravel?
- How to Drop Foreign Key Constraint in Laravel Migration?