Skip to content

Commit bd72299

Browse files
committed
Fixed update and insert methods when calling on relationships.
Fixes #360
1 parent 56581bb commit bd72299

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

src/Traits/Buildable.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,10 @@ public function inRandomOrder($seed = '')
114114

115115
public function insert(array $values)
116116
{
117-
$this->checkCooldownAndFlushAfterPersisting($this->model);
118-
117+
if (property_exists($this, "model")) {
118+
$this->checkCooldownAndFlushAfterPersisting($this->model);
119+
}
120+
119121
return parent::insert($values);
120122
}
121123

@@ -202,7 +204,9 @@ public function sum($column)
202204

203205
public function update(array $values)
204206
{
205-
$this->checkCooldownAndFlushAfterPersisting($this->model);
207+
if (property_exists($this, "model")) {
208+
$this->checkCooldownAndFlushAfterPersisting($this->model);
209+
}
206210

207211
return parent::update($values);
208212
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Integration\CachedBuilder;
2+
3+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Book;
4+
use GeneaLabs\LaravelModelCaching\Tests\IntegrationTestCase;
5+
6+
class UpdateRelationTest extends IntegrationTestCase
7+
{
8+
public function testInRandomOrderCachesResults()
9+
{
10+
$book = (new Book)
11+
->with("stores")
12+
->whereHas("stores")
13+
->first();
14+
$book->stores()
15+
->update(["name" => "test store name change"]);
16+
$updatedCount = (new Book)
17+
->with("stores")
18+
->whereHas("stores")
19+
->first()
20+
->stores()
21+
->where("name", "test store name change")
22+
->count();
23+
24+
$this->assertEquals(1, $updatedCount);
25+
}
26+
}

0 commit comments

Comments
 (0)