Skip to content

Commit c791a9c

Browse files
[8.x] Model Broadcasting - Adding broadcastWith() and broadcastAs() support (#7212)
* WIP * WIP * formatting and fixing bugs Co-authored-by: Taylor Otwell <[email protected]>
1 parent 21327ed commit c791a9c

File tree

1 file changed

+46
-2
lines changed

1 file changed

+46
-2
lines changed

broadcasting.md

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -900,9 +900,53 @@ $user->broadcastChannel()
900900
<a name="model-broadcasting-event-conventions"></a>
901901
#### Event Conventions
902902

903-
Since model broadcast events are not associated with an "actual" event within your application's `App\Events` directory, they are assigned a name based on convention. Laravel's convention is to broadcast the event using the class name of the model (not including the namespace) and the name of the model event that triggered the broadcast.
903+
Since model broadcast events are not associated with an "actual" event within your application's `App\Events` directory, they are assigned a name and a payload based on conventions. Laravel's convention is to broadcast the event using the class name of the model (not including the namespace) and the name of the model event that triggered the broadcast.
904904

905-
So, for example, an update to the `App\Models\Post` model would broadcast an event to your client-side application as `PostUpdated`, while the deletion of an `App\Models\User` model would broadcast an event named `UserDeleted`.
905+
So, for example, an update to the `App\Models\Post` model would broadcast an event to your client-side application as `PostUpdated` with the following payload:
906+
907+
{
908+
"model": {
909+
"id": 1,
910+
"title": "My first post"
911+
...
912+
},
913+
...
914+
"socket": "someSocketId",
915+
}
916+
917+
The deletion of the `App\Models\User` model would broadcast an event named `UserDeleted`.
918+
919+
If you would like, you may define a custom broadcast name and payload by adding a `broadcastAs` and `broadcastWith` method to your model. These methods receive the name of the model event / operation that is occurring, allowing you to customize the event's name and payload for each model operation. If `null` is returned from the `broadcastAs` method, Laravel will use the model broadcasting event name conventions discussed above when broadcasting the event:
920+
921+
```php
922+
/**
923+
* The model event's broadcast name.
924+
*
925+
* @param string $event
926+
* @return string|null
927+
*/
928+
public function broadcastAs($event)
929+
{
930+
return match ($event) {
931+
'created' => 'post.created',
932+
default => null,
933+
};
934+
}
935+
936+
/**
937+
* Get the data to broadcast for the model.
938+
*
939+
* @param string $event
940+
* @return array
941+
*/
942+
public function broadcastWith($event)
943+
{
944+
return match ($event) {
945+
'created' => ['title' => $this->title],
946+
default => ['model' => $this],
947+
};
948+
}
949+
```
906950

907951
<a name="listening-for-model-broadcasts"></a>
908952
### Listening For Model Broadcasts

0 commit comments

Comments
 (0)