You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -214,7 +212,7 @@ Route::post('posts/{post}/comments', function (Post $post) {
214
212
215
213
The `request()->wantsTurboStream()` macro added to the request will check if the request accepts Turbo Stream and return `true` or `false` accordingly.
216
214
217
-
Here's what that `comments.turbo.created_stream.blade.php` view could look like:
215
+
Here's what the HTML response will look like:
218
216
219
217
```blade
220
218
<turbo-stream action="append" target="comments">
@@ -224,87 +222,94 @@ Here's what that `comments.turbo.created_stream.blade.php` view could look like:
224
222
</turbo-stream>
225
223
```
226
224
225
+
Most of these things were "guessed" based on the [naming conventions](#conventions) we talked about earlier. But you can override most things, like so:
*`append` & `prepend`: to add the elements in the target element after the existing contents or before, respectively
230
243
*`replace`: will replace the existing element entirely with the contents of the `template` tag in the Turbo Stream
231
244
*`update`: will keep the target and only replace the contents of it with the contents of the `template` tag in the Turbo Stream
232
-
*`remove`: will remove the element. This one doesn't need a `<template>` tag.
245
+
*`remove`: will remove the element. This one doesn't need a `<template>` tag. It accepts either an instance of a Model or the DOM ID of the element to be removed as a string.
246
+
247
+
Which means you will find shorthand methods for them all, like:
248
+
249
+
```php
250
+
response()->turboStream()->append($comment);
251
+
response()->turboStream()->prepend($comment);
252
+
response()->turboStream()->replace($comment);
253
+
response()->turboStream()->update($comment);
254
+
response()->turboStream()->remove($comment);
255
+
```
233
256
234
257
You can read more about Turbo Streams in the [Turbo Handbook](https://turbo.hotwire.dev/handbook/streams).
235
258
236
-
If you notice, all we're doing in the `comments.turbo.created_stream.blade.php` view is wrapping the comment's partial with a Turbo Stream tag. You can alternatically delegate the generation of the Turbo Stream tag to a `response()->turboStream()` macro, which will essentially do the same thing, but in this case you wouldn't need that `comments.turbo.created_stream.blade.php` anymore:
259
+
These shorthand methods return a pending object for the responsewhich you can chain and override everything you want on it:
237
260
238
261
```php
239
-
Route::post('posts/{post}/comments', function (Post $post) {
As mentioned earlier, passing a model to the `response()->turboStream()` macro will pre-fill the pending response object with some defaults based on the model's state.
245
268
246
-
return back();
247
-
});
248
-
```
269
+
It will build a `remove` Turbo Stream if the model was deleted (or if it is trashed - in case it's a Soft Deleted model), an `append` if the model was recently created (which you can override the action as the second parameter of the macro), a `replace` if the model was just updated (you can also override the action as the second parameter.) Here's how overriding would look like:
249
270
250
-
Again, this will detect that your model was recently created and generate an `append` Turbo Stream to a `comments` target (using the plural name of your model's basename for that) and render the model's partial inside a `template` tag, similarly to what we were doing manually.
The `response()->turboStream()` macro will generate a _replace_ Turbo Stream action targeting your model's DOM ID when you are only updating the model. In the same way, if you have deleted the model, it will generate a _remove_ Turbo Stream also using the model's DOM ID as target.
275
+
<aname="custom-turbo-stream-views"></a>
276
+
### Custom Turbo Stream Views
253
277
254
-
If you're not using the model partial convention, you may stick with the `response()->turboStreamView()` version instead and specify your own Turbo Stream views. See the [conventions section](#ceventions) to read more about this.
278
+
If you're not using the model partial [convention](#conventions) or if you have some more complex Turbo Stream constructs, you may use the `response()->turboStreamView()` version instead and specify your own Turbo Stream views.
255
279
256
-
You may override the partial name by implementing a `hotwirePartialName` method in your model. You may also have more control over the data that is passed down to the partial by implementing the `hotwirePartialData` method, like so:
You may also override the resource name used as target in the case wher you're generating a Turbo Stream for recently created models, as well as the DOM ID that will be used as targets when your model was either updated or deleted by implementing the following methods:
288
+
And here's an example of a more complex custom Turbo Stream view:
Erlier I showed you a custom Turbo Stream view before I showed you how to auto-generate Turbo Stream views for you models. The name and location of that view was no accident. When auto-generating the Turbo Stream views for your model using the `response()->turboStream()` helper function, it will first check if you have a Custom Turbo Stream View in place for this model and, if so, it will use that view instead of generating one from scratch.
298
-
299
-
This way, you can have more control over your Turbo Stream responses. To use custom Turbo Stream views, you may create a `turbo` folder in the model's resource views folder and name them after the model event you want to override, like so:
300
+
Remember, these are Blade views, so you have the full power of Blade at your hands. In this example, we're including a shared Turbo Stream partial which could append any flash messages we may have. That `layouts.turbo.flash_stream` could look like this:
**Note: these will only be used when you're using the `response()->turboStream()` macro.**
312
+
I hope you can see how powerful this can be to reusing views.
308
313
309
314
<aname="broadcasting"></a>
310
315
### Broadcasting Turbo Streams Over WebSockets With Laravel Echo
@@ -640,7 +645,6 @@ public function store()
640
645
641
646
You may also catch the `ValidationException` and return a non-200 response, if you want to.
642
647
643
-
644
648
<aname="turbo-native"></a>
645
649
### Turbo Native
646
650
@@ -739,7 +743,7 @@ class CreatesCommentsTest extends TestCase
739
743
}
740
744
```
741
745
742
-
**Note: make sure your `turbo-laravel.queue` config key is set to false, otherwise actions may not be dispatched during test because the model observer only fires them after the transaction is commited, which never happens in tests since they run inside a transaction.**
746
+
*Note: make sure your `turbo-laravel.queue` config key is set to false, otherwise actions may not be dispatched during test because the model observer only fires them after the transaction is commited, which never happens in tests since they run inside a transaction.*
0 commit comments