Migrating / Importing MySQL DB Data from Custom PHP to Laravel (Seeding)

A couple of years ago we have worked on a project for a branch of financial institution, to track and generate reports on sales and activity of advisors. The HQ of the financial institution would produce weekly reports in excel format, the branch wanted to have something more visual and exciting then a chart of numbers…

So the web app we built has import function to import the excel report data into the database. The second part of the web app is to take this data and display nice reports with graphs comparing month to month and year to year in visually appealing manner.The web app was built with old version of Yii PHP framework backend and angular.js based front end. Over time more functions were added:

  • PDF generation
  • Weeks since last sale
  • emailing reports
  • etc…

All was great, except the developer who built this doesn’t work for us anymore, and none of us particularly like / specialize in Yii. Another issue that we noticed is something that happens to projects that starts small, but then continuously add functions, some of the original architecture decisions were hurting us.  So we made a decision that its better to “bite the bullet” now and rebuild it in a framework that we know and is vastly more popular today than continue with a framework we don’t know that well and have no experience working with, plus we will have an added benefit of having a fresh start while now understanding the full scope of the project much better. The front end wouldn’t need full reworking since angular works equally well with both PHP frameworks.

In Laravel creating a new table schema is handled by Migrations. The migrations are really well documented here, but lets just quickly go through the steps:

Step 1 – create migration: php artisan make:migration create_advisors_table --create=advisors After its done open the created file and add the fields that you require

Here what our file looked like:

Step 2 – run the migration: php artisan migrate

This should have created the DB table for you. Now the part that wasn’t so well documented, was Seeding. That is the Laravel tool that is used to seed the database.

PhpMyAdmin has a couple export options for DB tables, we have decided to export the old database in JSON format as its easy to work with it in PHP, convert to PHP array and iterate over it. In the database/seeds directory we have created json-dump directory and uploaded the exported json file there.

phpmyadmin json export

Next step is to create the migration by running the following: php artisan make:seeder AdvisorsTableSeeder find the created file and write in the seeding code in our case it looks like so:

All that’s left todo is run the migration: php artisan db:seed --class=AdvisorsTableSeeder

Note: If you get a “class not found” error when running migrations or seeding, you need to recreate composer autoload by issuing the following command: composer dump-autoload

Did this help you or do you have a better solution? don’t be shy let us know! 😉

Related Posts

No results found.

2 Comments. Leave new

  • Why didn’t you just run

    ?

    Reply
    • I was approaching it one step at a time, the part that “scared” me the most was importing the data, so thats what i started from, i figured that we will take care of the Model and Controller once the data is migrated…

      But you are right, your way would have worked 🙂

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu