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?
Let's assume your akun table has a model Akun attached. We assume the same thing for master_lookups (MasterLookup).
You can try something like:
Akun::all()->each(function($akun) {
$masterLookup = new MasterLookup;
$masterLookup->code = $akun->code;
$masterLookup->name = $akun->name;
$masterLookup->save()
}
Depending on the number of rows you have, you might want to use the chunk
method instead of all
to avoid running out of memory when trying to grab all the items.