jeudi 12 avril 2018

Seeding a pivot table in Laravel 5 using factory

    public function run()
    {
    factory(App\ProjectProcurementManagementPlan::class, 5)->create()->each(function ($a) {
        $a->paps()
            ->saveMany( factory(App\ProjectsProgramsActivities::class, 5)->make() )
            ->each(function ($b) {
                $b->pap_schedules()->save(factory(App\PapSchedule::class)->make());
                $b->procurement_modes()-> ????????;
            });
        });
    }

I have this code here. I want to seed a pivot table after creation of each of the models indicated above. $b->procurement_modes()-> ???????? will be the part where I do the factory stuff to a pivot table.

the pivot table looks like this.

| id | pap_id | procurement_mode_id |

My plan is, to , for the sake of simplicity, I'll just attach a single procurement_mode_id to each ProjectsProgramsActivities created.

I have tried to use

$b->procurement_modes()->sync(factory(App\PAPProcurementMode::class)->make());

but it gives me a

SQLSTATE[HY000]: General error: 1366 In correct integer value: '' for column 'procurement_mode_id' at row 1 (SQL: insert into paps_procurement_modes (pap_i, procurement_mode_id) values (1, ))

The factory for that pivot table is this

    $factory->define(App\PAPProcurementMode::class, function (Faker $faker) {
    return [
        'procurement_mode_id' => 1,
    ];
});

So what would be the best way to do this ?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire