Skip to content

Commit 8449da6

Browse files
authored
Merge pull request #109 from hotwired-laravel/fix-not-broadcasting-when-using-model-touching
Fix model broadcast was not being triggered when using model touching
2 parents 8a27373 + 97aad12 commit 8449da6

File tree

4 files changed

+87
-13
lines changed

4 files changed

+87
-13
lines changed

src/Models/ModelObserver.php

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,17 @@ public function __construct()
2121
/**
2222
* @param Model|Broadcasts $model
2323
*/
24-
public function created(Model $model)
24+
public function saved(Model $model)
2525
{
2626
if (! $this->shouldBroadcast($model)) {
2727
return;
2828
}
2929

30-
$model->broadcastInsert()->later();
31-
}
32-
33-
/**
34-
* @param Model|Broadcasts $model
35-
*/
36-
public function updated(Model $model)
37-
{
38-
if (! $this->shouldBroadcast($model)) {
39-
return;
30+
if ($model->wasRecentlyCreated) {
31+
$model->broadcastInsert()->later();
32+
} else {
33+
$model->broadcastReplace()->later();
4034
}
41-
42-
$model->broadcastReplace()->later();
4335
}
4436

4537
/**

tests/Models/BroadcastsModelTest.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
namespace Tonysm\TurboLaravel\Tests\Models;
44

55
use Illuminate\Broadcasting\Channel;
6+
use Illuminate\Database\Eloquent\Model;
67
use Illuminate\Support\Facades\Bus;
8+
use Illuminate\Support\Facades\View;
79
use Tonysm\TurboLaravel\Broadcasting\PendingBroadcast;
810
use Tonysm\TurboLaravel\Facades\TurboStream;
911
use Tonysm\TurboLaravel\Jobs\BroadcastAction;
@@ -17,6 +19,8 @@ protected function setUp(): void
1719
{
1820
parent::setUp();
1921

22+
View::addLocation(__DIR__ . '/../Stubs/views');
23+
2024
config(['turbo-laravel.queue' => false]);
2125
}
2226

@@ -436,6 +440,43 @@ public function using_the_new_broadcast_fake()
436440
return true;
437441
});
438442
}
443+
444+
/** @test */
445+
public function broadcasts_on_model_touching()
446+
{
447+
TurboStream::fake();
448+
449+
$oldUpdatedAt = now()->subDays(10);
450+
451+
$post = Post::withoutEvents(fn () => Post::forceCreate([
452+
'title' => 'Testing Model Touching',
453+
'updated_at' => $oldUpdatedAt,
454+
]));
455+
456+
$this->assertTrue($post->fresh()->updated_at->isSameDay($oldUpdatedAt));
457+
TurboStream::assertNothingWasBroadcasted();
458+
459+
$post->fresh()->comments()->create([
460+
'body' => 'Hello World',
461+
]);
462+
463+
// Must have updated the parent timestamps...
464+
$this->assertFalse($post->fresh()->updated_at->isSameDay($oldUpdatedAt));
465+
466+
TurboStream::assertBroadcasted(function (PendingBroadcast $broadcast) use ($post) {
467+
$this->assertCount(1, $broadcast->channels);
468+
$this->assertEquals('private-' . $post->broadcastChannel(), $broadcast->channels[0]->name);
469+
$this->assertEquals(dom_id($post), $broadcast->target);
470+
$this->assertNull($broadcast->targets);
471+
$this->assertEquals('replace', $broadcast->action);
472+
$this->assertEquals('posts._post', $broadcast->partialView);
473+
$this->assertCount(1, $broadcast->partialData);
474+
$this->assertArrayHasKey('post', $broadcast->partialData);
475+
$this->assertTrue($post->is($broadcast->partialData['post']));
476+
477+
return true;
478+
});
479+
}
439480
}
440481

441482
class BroadcastTestModel extends TestModel
@@ -525,3 +566,29 @@ public function parent()
525566
return $this->belongsTo(RelatedModelParent::class);
526567
}
527568
}
569+
570+
class Post extends Model
571+
{
572+
use Broadcasts;
573+
574+
protected $guarded = [];
575+
576+
protected $broadcasts = ['insertsBy' => 'prepend'];
577+
578+
public function comments()
579+
{
580+
return $this->hasMany(Comment::class);
581+
}
582+
}
583+
584+
class Comment extends Model
585+
{
586+
protected $guarded = [];
587+
588+
protected $touches = ['post'];
589+
590+
public function post()
591+
{
592+
return $this->belongsTo(Post::class);
593+
}
594+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div>{{ $post->title }}</div>

tests/TestCase.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,19 @@ private function setUpDatabase(Application $app): void
4545
$table->timestamps();
4646
$table->softDeletes();
4747
});
48+
49+
$app['db']->connection()->getSchemaBuilder()->create('posts', function (Blueprint $table) {
50+
$table->id();
51+
$table->string('title');
52+
$table->text('body')->nullable();
53+
$table->timestamps();
54+
});
55+
56+
$app['db']->connection()->getSchemaBuilder()->create('comments', function (Blueprint $table) {
57+
$table->id();
58+
$table->foreignId('post_id');
59+
$table->text('body');
60+
$table->timestamps();
61+
});
4862
}
4963
}

0 commit comments

Comments
 (0)