Mark Karavan Mark Karavan - 1 year ago 55
PHP Question

Why isn't PHP/Laravel incrementing this value?

Unit testing with Laravel 5 and sqlite. I have a simple test where a

creates a

/** @test */
public function it_should_increment_post_count_when_a_user_makes_a_post()
// Given
$u = User::first();
$count1 = $u->posts->count();

// When
$p = new Post();
$p->title = 'My Title';
$p->content = 'This is the content';
$p->user_id = $u->id;
$p->save(); // This is definitely saving
$count2 = $u->posts->count();

// Then
$this->assertEquals($count1+1, $count2);

The results are as follows:

root@7053bd7f558c:/var/www/laravel# phpunit
PHPUnit 5.5.7 by Sebastian Bergmann and contributors.

. 7 / 7 (100%)

Time: 1.98 seconds, Memory: 12.00MB

There was 1 failure:

1) BasicUserTest::it_should_increment_post_count_when_a_user_makes_a_post
Failed asserting that 11 matches expected 12.


Tests: 7, Assertions: 11, Failures: 1.

It is certainly saving to the database (if I run this without
use DatabaseTransactions;
it increments every time). Is
a reference to

A similar variation yields a strange property:

/** @test */
public function it_should_increment_post_count_when_a_user_makes_a_post()
// Given
$p = new Post([
'title' => 'tha title',
'content' => 'tha content',
$u_old = User::first();

// When

// Then
$u_new = User::first();
$this->assertEquals($u_old->posts->count()+1, $u_new->posts->count());

This works, but if I comment out the line
, it fails.

Answer Source

count keep the same value because the value is stored on the Eloquent model. This is a value kept in memory.

So, after insert, you need to make a new request. For example: <?php DB::table('name')->count(); ?>

Or may by just a real count: <?php $count2 = count($y->posts); ?>

(I don't remember if there is a tips to reload the counter value ...)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download