gonziis - 6 months ago

Im a bit confused about how to seed anomaly.field_type.multiple fields with data.

$adresats = DB::connection('mysql_live')->select("select * from decisions");

$this->adressats->truncate();

foreach ($adresats as $key => $value) {
    if($value->d1_category_id == null) continue;
    $this->adressats->create([
        'lv' => ['name' => $value->title],
        'en' => ['name' => $value->title],
        'slug' => str_slug($value->title),
        'enabled' => $value->enabled,
        'lemumu_categories_id' => $value->d1_category_id,
        'd_nr' => $value->d_nr,
        'date_a' => $value->date_a,
        'date_b' => $value->date_b,
        'pub_info' => $value->pub_info,
        'gads' => $value->gads,
        'state_1' => $value->state_1,
        'state_2' => $value->state_2,
        'state_3' => $value->state_3,
        'state_4' => $value->state_4,
        'state_5' => $value->state_5
    ]);
}

I am seeding successfully from old project database as you can see. But I have only included the fields with simple values, only the 'lemumu_categories_id' is a single relationship field. But the thing is, what method to use to seed a many-to-many relationship field? It isn't as simple as assigning a value. Could you help a little bit? Thanks.

ryanthompson - 6 months ago

The field type provides a native Laravel BelongsToMany relation so you can use that to attach / sync like you would with Laravel.