Search code examples
sqllaravellaravel-9laravel-migrationsrawsql

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?


Solution

  • 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'));
    }