samedi 7 décembre 2019

Laravel - Why in my factory the associate works before but not after a create is done?

I'm having difficulty to understand why does in this code the winner is correctly associated and saved to database:

$factory->afterCreatingState(Match::class, 'done', function (Match $match, Faker\Generator $faker) {
    $match->winner()->associate($match->team1)->save();

    factory(Game::class)->create([
        'match_id' => $match->id
    ]);
});

But when the association is done after, winner is somehow reset to null when launching the save and as a result it is not properly saved to the database (but everything else is fine)?

$factory->afterCreatingState(Match::class, 'done', function (Match $match, Faker\Generator $faker) {
    factory(Game::class)->create([
        'match_id' => $match->id
    ]);

    $match->winner()->associate($match->team1)->save();
});

If someone could enlight me, thanks.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire