Support the ongoing development of →
Database Testing Installation
Last updated 2 years ago.

Are you using database transactions trait?


@simonhunt I removed it so I could observe the entries into the database.


@olimorris Sorry to come in again with more obvious questions but here goes:

Are you using a separate testing database? Are you doing a migrate:refresh before each test? Do you have any other tests lurking in other folders? What is actually in the database?



I have experienced the same problem. When you create the job:

$job = factory(App\Job::class)->create([
   'org_id' => $org->id,
   'user_id' => $user->id

This lines inside the factory run anyway:

....'org_id' => factory(App\Organisation::class)->create()->id,
....'user_id' => factory(App\User::class)->create(['type' => 'organisation'])->id,

So it's creates the default Organization and User inside the database and only then it overwrites the 'org_id' and 'user_id' with the id's of models created outside the factory.

Hence you got 2 Organizations and 2 Users.

Last updated 7 years ago.

Gotta read the docs carefully, gentlemen; I just feel into this trap myself. Under "Relations & Attribute Closures", it clearly tells us to wrap the factory call in a closure. So instead of

'user_id' => factory(App\User::class)->create(['type' => 'organisation'])->id,

you should have

'user_id' => function () {
    return factory(App\User::class)->create(['type' => 'organisation'])->id;

The latter does allow to overwrite attributes, the former doesn't.

Hope this helps some desperate soul out there.

Last updated 6 years ago.

Thanks Alex. Desperate soul saved.


Sign in to participate in this thread!


Your banner here too?

olimorris olimorris Joined 31 Dec 2014


We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 - All rights reserved.