How to Migrate SQL dump file with Laravel

Last Updated at : January 29, 2018 By - Usama Muneer | 2715

    Database Migrations are no doubt the best thing about Laravel, where your PHP code transforms into SQL with so less effort than usual. But what if you wanted to write a migration for a SQL dump file? Laravel has a way around for that too. I wouldn’t call it a proper solution. But it works great.

    Step # 01

    Create a normal migration file using artisan CLI

    php artisan make:migration create_my_table_migration --create=my_table

     

    Step # 02

    Inside the up method of your migration file, copy & paste the below code

    \DB::unprepared( file_get_contents( "link/to/dump.sql" ) );

     

    Additional Tip

    This works great for smaller dump files, but what if you’ve got a large file to migrate? We got you covered. It’s a bit dirtier but works just fine.

    Inside of your up method, before the linking to SQL file, add the following line of code.

    ini_set('memory_limit', '-1');
    \DB::unprepared( file_get_contents( "link/to/dump.sql" ) );

    You can also set memory limit inside your PHP.ini file. Totally up to you. But this saves a bunch of time i guess.

    blog user
    Usama Muneer

    A web enthusiastic, self-motivated & detail-oriented professional Full-Stack Web Developer from Karachi, Pakistan with experience in developing applications using JavaScript, WordPress & Laravel specifically. Loves to write on different web technologies with an equally useful skill to make some sense out of it.

    Related Posts

    Importing Excel files come handy when you’re building some large scale inventory kind of an application where you need to import set of data from CSV or excel files. In this short tutorial, we’ll go through on importing excel files with Laravel & look on for some crazy hacks and tricks in between.

    Gear up fellow developers, another Laravel Meetup is coming back to Lahore & it will be a great milestone while helping our fellow web developers across the city to become motivated and will help them grow as Laravel developers.

    Getting an app developed is not the only thing that one needs to ensure in terms of digital visibility and e-commerce. Yes, having an app is important but to stay updated and to be abreast with the competition Optimization of performance is necessary.

    Telescope gives knowledge into the solicitations coming into your application, exemptions, log sections, database questions, lined employments, mail, notices, reserve activities, planned errands, variable dumps and that's just the beginning.