CrimsonJazz's avatar

Seeding multiple static data

Good day, until now I have been seeding static data into my DB using the following code on my seeders:

public function run(){ $farms = array( "N/A", "Farm 1", "Farm 2", "Farm 3" );

    foreach ($farms as $farm){
        Farm::create([
            'name' => $farm
        ]);
    }
}

And it has been working perfectly, but now I'm trying to use the same code to seed multiple static data in a new table (using a different seeder):

public function run() { $varieties = array( "Plant 1", "Plant 2", "Plant 3" );

	$farms = array(
        "1",
        "2",
        "3"
    );

	$crops = array(
        "2019",
        "2020",
        "2021"
    );

	foreach ($varieties as $variety){
        	Plant::create([
            'variety'       	=> $variety,
            'farm_id'       => $farms,
            'crop'          	=> $crops
        ]);
    }
}

But now Im getting the error:

Array to string conversion

Illuminate\Foundation\Bootstrap\HandleExceptions::Illuminate\Foundation\Bootstrap{closure}("Array to string conversion"

Illuminate\Support\Str::replaceArray("?", "insert into [plants] ([variety], [farm_id], [crop], [updated_at], [created_at]) values (?, ?, ?, ?)")

Any help will be appretiated.

0 likes
4 replies
AungHtetPaing__'s avatar
Level 22

@crimsonjazz you are giving array for farm_id and crop. So how about this

foreach ($varieties as $key => $variety){
        	Plant::create([
            'variety'       	=> $variety,
            'farm_id'       => $farms[$key],
            'crop'          	=> $crops[$key]
        ]);
    }
3 likes
NoLAstNamE's avatar

@CrimsonJazz because you're trying to save an array of $farms to a numeric farm_id column, adding $key only returns an id.

2 likes

Please or to participate in this conversation.