Run raw insert query or SQL file in Laravel seeder

Hey, whats up!
I am working to developed a base structure for one of my SAAS application and the scenario is we need to seed good amount of data wither through Laravel seeded or from sql dump file. So, I want to do it with both – I want to run the SQL dump file through the Laravel seeder. So, here is the solution

First create a seeder file by artisan command
php artisan make:seeder SqlSeeder

It will create a seeder file in database\seeds\SqlSeeder.php

And now put the below code into run() function in the SqlSeeder.php file
$path = base_path() . '/database/seeds/data.sql';
$sql = file_get_contents($path);

$path: is the path of the dump data.sql file
$sql: storing data.sql file’s content (sql query) through file_get_contents() function.
And executing query at the 3rd line.

Now run the artisan command
php artisan db:seed --class=SqlSeeder

***Magic*** 😀
Now check the database and thanks me by sharing and comment on this article. 😉

Inspire Me :)

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.