dimanche 9 décembre 2018

Laravel Eloquent relationship object stale even though data correct in database

I'm using Laravel 5.7 and have a one-to-one relationship between 2 eloquent models.

I have this simple function that works well, and the correct values persist to the database:

public function saveMarketingOriginInfo(Contact $contact, $data) {
    $contact->marketingOrigin()->create($data);        
    $this->makeOtherChangesByReference($contact->marketingOrigin);
    $contact->marketingOrigin->save();
    return $contact->marketingOrigin; 
}

However, when writing a functional test for it, I noticed that the object that it returns is stale (doesn't have the correct values in its properties).

My tests only pass if I change the return statement to return \App\Models\MarketingOrigin::find($contact->id);.

(MarketingOrigin uses 'contact_id' as primary key.)

What am I doing wrong?

How can I return the same object that was just saved in the previous line ($contact->marketingOrigin->save();) without making a database read query (find())?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire