@tobyreed can you post code for the test?
You may be only creating the Process object in memory and not saving it yet, or alternatively deleting the saved record once your test has ran.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi,
In my tests I am using a factory for my "process" but the issue is when I am running my test it does not throw any errors and when I dd the model that is created it returns all of the data for the model:
App\Models\Process^ {#5657}
data here...
However, when I try to query the database using a simple Process::all() to see if it's there, and this returns this:
Illuminate\Database\Eloquent\Collection^ {#5974
#items: []
}
I think it could be due to namespacing but it all seems to be correct. Any help is appreciated!
@tobyreed can you post code for the test?
You may be only creating the Process object in memory and not saving it yet, or alternatively deleting the saved record once your test has ran.
@automica if what you're saying is am I using the ->make() instead of ->create() I am not. I am using the
->create() method since it persists the data which I need.
And I don't think so, I am creating the Process object in the setUp method which I have used before and it's been working fine.
@tobyreed still, it would be helpful to see the actual code.
@automica here is my setup method:
protected function setUp(): void
{
parent::setUp();
$this->workspace = factory(Workspace::class)->create();
$this->workspace->setStatus('Active');
$this->process = factory(Process::class)->create(['workspace_id' => $this->workspace->id]);
$this->uniqueProcess = new UniqueProcess($this->workspace);
$this->faker = Factory::create();
}
and here is my test code:
/** @test */
public function should_fail_process_slug_not_unique_and_test_correct_error_message()
{
$this->assertFalse((bool)
$this->uniqueProcess->passes('slug', $this->process->slug),
'Should fail using same slug'
);
$this->assertSame(
'The process "'.$this->process->slug.'" should be unique in the workspace',
$this->uniqueProcess->message()
);
}
@tobyreed what does your UniqueProcess class look like?
@automica that class is the class I am testing and it's a validation rule
@tobyreed if you change this to
$this->assertTrue((bool)
$this->uniqueProcess->passes('slug', $this->process->slug),
'Should fail using same slug'
);
are you then getting a fail?
seeing whats going on in the validation rule would be helpful.
@automica the issue is partially that the test fails. But the main issue and the cause of the test failing is because of the factory, I am able to do this: dd($this->process) and it will return me a Process object. But if I do a dd(Process::all) then it will return an empty array.
The ->passes() method is used to test the data provided and I am checking that it should fail when trying to create two processes with the same slugs when they should be unique.
Hi all
is there a solution about this issue ? I got same problem about factory that sometimes real write to db and i can get it but sometimes failed. here the some result when i test on tinker.
>>> Product::find(Product::factory()->hasRelatedPrices(1)->create(['product_stock' => 1])->product_id)
=> null
>>> Product::find(Product::factory()->hasRelatedPrices(1)->create(['product_stock' => 1])->product_id)->product_id
=> "46dc963e-3c8b-4d8f-bb53-b070eb141c2a"
>>> Product::find(Product::factory()->hasRelatedPrices(1)->create(['product_stock' => 1])->product_id)->product_id
<warning>PHP Warning: Attempt to read property "product_id" on null in F:\laragon\www\tokocuan-mobile-apieval()'d code on line 1</warning>
=> null
>>> Product::find(Product::factory()->hasRelatedPrices(1)->create(['product_stock' => 1])->product_id)->product_id
<warning>PHP Warning: Attempt to read property "product_id" on null in F:\laragon\www\tokocuan-mobile-apieval()'d code on line 1</warning>
=> null
>>> Product::find(Product::factory()->hasRelatedPrices(1)->create(['product_stock' => 1])->product_id)->product_id
<warning>PHP Warning: Attempt to read property "product_id" on null in F:\laragon\www\tokocuan-mobile-apieval()'d code on line 1</warning>
=> null
>>> Product::find(Product::factory()->hasRelatedPrices(1)->create(['product_stock' => 1])->product_id)->product_id
=> "83abd4e6-9c93-4be0-a098-b6785bb35e33"
>>> Product::find(Product::factory()->hasRelatedPrices(1)->create(['product_stock' => 1])->product_id)->product_id
<warning>PHP Warning: Attempt to read property "product_id" on null in F:\laragon\www\tokocuan-mobile-apieval()'d code on line 1</warning>
=> null
>>> Product::find(Product::factory()->hasRelatedPrices(1)->create(['product_stock' => 1])->product_id)->product_id
=> "41f06e06-7dfb-49a1-bec4-488d88e15fd0"
>>> Product::find(Product::factory()->hasRelatedPrices(1)->create(['product_stock' => 1])->product_id)->product_id
=> "2a9c3644-6a15-4b39-8af8-1a0602ff7222"
>>> Product::find(Product::factory()->hasRelatedPrices(1)->create(['product_stock' => 1])->product_id)->product_id
<warning>PHP Warning: Attempt to read property "product_id" on null in F:\laragon\www\tokocuan-mobile-apieval()'d code on line 1</warning>
=> null
>>> Product::find(Product::factory()->hasRelatedPrices(1)->create(['product_stock' => 1])->product_id)->product_id
=> "63b699e4-3279-4fd3-b1bf-944ef6c66d13"
>>> Product::find(Product::factory()->hasRelatedPrices(1)->create(['product_stock' => 1])->product_id)->product_id
=> "ce691c46-6537-4e2a-a567-e279d7d9cd14"
>>> Product::find(Product::factory()->hasRelatedPrices(1)->create(['product_stock' => 1])->product_id)->product_id
=> "17c3c21c-ec70-4e20-a7d0-344980567766"
>>> Product::find(Product::factory()->hasRelatedPrices(1)->create(['product_stock' => 1])->product_id)->product_id
<warning>PHP Warning: Attempt to read property "product_id" on null in F:\laragon\www\tokocuan-mobile-apieval()'d code on line 1</warning>
=> null
>>> Product::find(Product::factory()->hasRelatedPrices(1)->create(['product_stock' => 1])->product_id)->product_id
=> "076dfecf-b1ad-4da7-8af0-6aa797294978"
>>> Product::find(Product::factory()->hasRelatedPrices(1)->create(['product_stock' => 1])->product_id)->product_id
=> "959f4645-ca3d-48ca-993a-cc088c8eb58e"
>>> Product::find(Product::factory()->hasRelatedPrices(1)->create(['product_stock' => 1])->product_id)->product_id
<warning>PHP Warning: Attempt to read property "product_id" on null in F:\laragon\www\tokocuan-mobile-apieval()'d code on line 1</warning>
=> null
Try to use \DB::commit() in your test.
Please or to participate in this conversation.