Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 9: how to insert data using raw sql while doing a migration?

I've a "static" table I need to import while doing initial migrations on my project.

I've a .sql file. It's a lot of data, > 35k records

My idea was to create table in a standard migration and then, with "something" execute the insert sql I already have in the separate file.

Is it possible? How?

like image 726
realtebo Avatar asked Oct 13 '25 07:10

realtebo


1 Answers

You can add this in another migration file.

use Illuminate\Support\Facades\DB;

public function up()
{
    // ini_set('memory_limit', '-1'); <-- you can add this in case the file is big
    DB::unprepared(file_get_contents('path/to/dump.sql'));
}
like image 110
Kenny Horna Avatar answered Oct 14 '25 21:10

Kenny Horna