moses's avatar
Level 2

How to import data from one table to another table using Database: Seeding? (laravel 5.3)?

My DatabaseSeeder is like this :

<?php

use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;

class DatabaseSeeder extends Seeder
{
    public function run()
    {
        Model::unguard();

        $this->call('MasterLookupsTableSeeder')
    }
}

My MasterLookupsTableSeeder is like this :

<?php

use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;

class MasterLookupsTableSeeder extends Seeder
{
    public function run()
    {
        DB::table('master_lookups')->insert([
            'id'            =>  '17',           
            'code'          =>  '002',
            'name'          =>  'sample data',
            'information'   =>  NULL,
            'created_at'    =>  date('Y-m-d H:i:s'),
            'updated_at'    =>  date('Y-m-d H:i:s')
        ]);
    }
}

When I run php artisan db:seed --class=MasterLookupsTableSeeder, it will insert into to table master_lookups. It is still static. I want to create a dynamic where the data is inserted into master_lookups table taken from another table, for example akuns table. akuns table have field id, code, name, code_shopping.

I want like this :

code data from akuns table insert to field code in table master_lookups

name data from akuns table insert to field name in table master_lookups

code_shopping data from akuns table insert to field information in table master_lookups in the form of json. data type of field information is json.

Is there any people who can help me?

0 likes
3 replies
andonovn's avatar

You will have to write the code by your own.

If the "akuns" table already has data and you just want to transfer it via seeder, all you need is one simple foreach statement:

  • Select the data from the "akuns" table
  • Loop over it and insert the rows to the master_lookups table

If you want to dynamically insert data in both of the tables, you can take advantage of the Model Factories ( https://laravel.com/docs/5.3/database-testing#writing-factories ) to define factory for the "akuns" table and then use it in your seeder like this:

factory(App\Akun::class, 50)->create()->each(function ($akun) {
    DB::table('master_lookups')->insert([     
        'code'              =>  $akun->code,
        'name'             =>  $akun->name,
        'information'   =>  json_encode($akun->code_shopping),
    ]);
});

Hope that makes sense?

moses's avatar
Level 2

@andonovn, I change to be : factory(App\Model\Akun::class, 50)-> ... Because my model akun in folder model. Then, I run : php artisan db:seed --class=MasterLookupsTableSeeder, There is exist error like this : [InvalidArgumentException] Unable to locate factory with name [default] [App\Models\Akun].

andonovn's avatar

@moses because you haven't defined the factory for this model.

Go to database/factories/ModelFactory.php and add something like this

$factory->define(App\Model\Akun::class, function (Faker\Generator $faker) {
    return [
        'code' => $faker->numberBetween(0, 9999),
        'name' => $faker->word,
        'code_shopping' => $faker->sentence,
    // your other fields in the akuns table
    ];
});

Note I am writing code directly here without even code highlight so there might be some syntax error. The important thing is to define the model factories in your ModelFactory.php file and then use it from within your seeders, or tests, or wherever you need dummy data.

Please or to participate in this conversation.