Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rory's avatar
Level 1

Populate database in Laravel

This may sound a very trivial question but really I do not get how to populate mySQL database. Yes, there are many many tutorials out there showing how to insert manually data into table, or how to construct a form to make the user insert his/her data. My data are actually stored in many variables (array, lists...) and I need to put each one of this array in a column in my table. Why I'm not able to find any example of how to insert existing data from variables into database? Should I first save all this data to a file(excel, csv)? Am I doing something wrong in the way I think a database should be populated? What is the best procedure/the normal proceeding to add existing data to a database table?

0 likes
15 replies
biishmar's avatar

@rory it's better to create those in sql file and u can run those using mysql cmd source command its fast.

1 like
36864's avatar

If you read that thread again, you'll see that people use seeders in production all the time.

Seeders are a great way to set up default values in your database, such as roles and permissions, for example.

Most tutorials out there show you how to take data from the user, or from an API, and then put it into the database.

"normal" data flow is User > PHP variables > Database. In your case, you can skip that first step and just put the data you have in the database.

1 like
rory's avatar
Level 1

@36864 thanks a lot! I'm trying to use seeder now....just figuring out where to put my code and from where can read variables...as you said there are few tutorials about this, maybe it is just too simple and I'm not seeing it.

Thyrosis's avatar

I've recently migrated away from my WordPress blog to one I've built myself using Laravel. For this, I've used seeders to copy the database:

<?php

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // And then the WordPress posts and comments
        DB::unprepared(File::get(base_path() . '/database/seeds/wpposts.sql'));
        DB::unprepared(File::get(base_path() . '/database/seeds/wpcomments.sql'));
        DB::unprepared(File::get(base_path() . '/database/seeds/wpterms.sql'));
        DB::unprepared(File::get(base_path() . '/database/seeds/wptags.sql'));
        DB::unprepared(File::get(base_path() . '/database/seeds/wppostterms.sql'));
    }
}

The SQL files are just plain SQLdumps/exports from the WP-database, retrieved using PHPMyAdmin, added to the database folder and called by running

php artisan migrate --seed

That's all there is to it :)

2 likes
Cronix's avatar

@Thyrosis The problem with having production data in seeders, is if you ever run the seeder again(php artisan migrate --seed), you'll reinsert all of that data again causing duplicates. I like putting production data in the actual migration files just after Schema::create(), so it will only ever run once when the migration is run.

3 likes
jrdi's avatar

Hi,

Sorry for my english, I'll try to do my best.

I can see @Cronix 's point. What's more there is another problem, I guess. Rollback a particular migration or create a new one might deprecate your seed data. For example, if you rollback a new table migration then your seed logic that populates this table will fail. Another example would be if you modify a table throught a new migration and you don't update your seed file. That would might fail, too. Generally, you must have your "migrations", "current data in your database" and "seed data" syncronized all the time.

I use the @Cronix 's method but I usually split up the migration into two different migration files. The first one which defines the table schema and the second one to populated it (with a down method which deletes this default data) thus you can rollback only your data and keep your table's schema.

rory's avatar
Level 1

Thanks @Cronix and @jurios. I think I'll also put all in my migration, the idea of two separated files would be the best solution for me.

rory's avatar
Level 1

@jurios I have another question, once you make the second migration file to populate your table how do you connect the correspondent table schema?

Cronix's avatar

@rory For me, I just have a .sql dump of the table, and import it like

DB::unprepared(file_get_contents(base_path('database/migrations/seeds/thisMigration.sql')));

I usually just put them in the migration file though

public $tableName = 'tableName';

public function up()
{
    // create the table
    Schema::create($this->tableName, function (Blueprint $table) {
        $table->increments('id');
        // other fields
    });

    // seed it
    DB::unprepared(file_get_contents(base_path('database/migrations/seeds/thisMigration.sql')));
}

public function down()
{
    Schema::dropIfExists($this->tableName);
}
3 likes
Hashim's avatar

@Cronix Do you still use this method 4 years on? Have you encountered any problems with it?

hassanalami's avatar

@jurios As said above, integrating data using a source (sql, json, or csv) should be much faster than doing it using the Querybuilder, if you have a lot of data...

1 like

Please or to participate in this conversation.