Laravel Import Large SQL File using Seeder Example
Jan 28, 2021
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....
- 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