vendredi 29 avril 2016

How to refresh values in laravel models where enums are replaced with values?

So I came across a weird issue while writing tests in laravel using factories. So this is a test I wrote:

/**
 @test
 */
public function document_belongs_to_a_patent()
{
    $patent = factory(Patent::class)->create();

    $document = factory(Document::class)->create([
        'documentable_id' => $patent->id,
        'documentable_type' => 'patent'
    ]);

    $this->assertArraySubset($patent->toArray(), $document->documentable->toArray());
}

So this should work, right because both should return the same thing and patent array should be equal or a subset of documentable array. But it was failing when I realised that there is an enum field in Patent model to which I am passing the value 1 but it was converted to the enum equivalent value in the database and when I tried document->documentable->toArray() it came back with the enum value rather than 1 which got me thinking how can make the model factory return the actual enum value and not the index number.

Top of the head I just fetched the patent just after creating it via the factory like so:

$patent = Patent::find($patent->id);

And it works well but it seems inconsistent. Is there a way to refresh models. I know we can refresh relationships of models but is there a way to do for the models themselves?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/26zFAjx
via IFTTT

Aucun commentaire:

Enregistrer un commentaire