تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Introducing Model Broadcasting0:00

Up until now we've looked at ways that we can manually broadcast our events from the back end to the front end. Whether that's utilizing the shouldBroadcast interface on our event classes or invoking the broadcast facade directly. Today I'd like to look at a slightly more automated approach, model broadcasting. Now as you can see here Laravel will actually allow us to use a trait called BroadcastsEvents and when this trait is used it will broadcast events automatically from the back end of our application to the front end in line with various state changes in our Eloquent models. As you can see here to do that we need to define a broadcastOn method. The broadcastOn method simply determines which channels we would like our events to be broadcast to the front.

do that we need to define a broadcastOn method. The broadcastOn method simply determines which channels we would like our events to be broadcast to the front end. And because the broadcastOn method receives the event we can actually determine which events in the lifecycle of our Eloquent model we actually want to act on. So in this case you can see we don't want to broadcast when the deleted event is invoked but for every other event we want to broadcast. Back here in our application I'm just going to click the plus button next to channels and you can see that an input is rendered on the page which will allow us to create a new channel. What we want to do here is hook into the create lifecycle event of our eloquent model, our channel eloquent model, and when that

Configuring Channel Model Events1:15

us to create a new channel. What we want to do here is hook into the create lifecycle event of our Eloquent model, our Channel Eloquent model, and when that event is triggered we want to dispatch it to the front end automatically using model events. Okay so this form is really really simple when the User presses the enter button we are going to invoke this createChannel method. That method receives the name of the channel and we're literally going to use it just to create a new channel directly using Eloquent. The first thing we need to do here is update our Eloquent model and we want to tell it to broadcast events. So by default now Laravel is going to broadcast every event of every lifecycle hook of the Eloquent model to our front end. We don't want to do that, we only

by default now Laravel is going to broadcast every event of every lifecycle hook of the Eloquent model to our front end. We don't want to do that, we only want to do that on create. So let's jump down to the bottom of our file and implement the broadcastOn method. Here we go so we take public function broadcastOn and we receive a string event and we want to do something similar to what was in the docs. We want to return a match statement here so we're going to say return match and we're going to pass in the event and if the event is created we want to dispatch on our presence channel existing presence channel with the name of workspace. The one that we're using already to render our list of online users. Okay and then by default for

Listening in Alpine Echo2:35

channel existing presence channel with the name of workspace. The one that we're using already to render our list of online users. Okay and then by default for everything else we want to do nothing so actually we're going to return an empty array here. Okay and that's literally all we need to do. We implement one trait and we implement a broadcastOn method which tells Laravel which channels we are actually wanting to broadcast these events. In our workspace component we now want to go and listen for that event so we can as we have done in other places in our codebase implement an init method of our Alpine component and as usual we can say echo join and we want to join workspace again and we want to listen for an event. Now this is where it gets a little bit more interesting.

usual we can say echo join and we want to join workspace again and we want to listen for an event. Now this is where it gets a little bit more interesting because typically when we are working with actual class-based events in our application Echo is expecting to receive them with the app\events namespace so the event that actually gets sent would be app\events\message_sent but with Eloquent models it's a little bit different. We're actually only going to receive an event called channel_created without the namespace at the beginning and with Echo we can get around that by prefixing the event name with a period. So the event name we want to listen to is .channel_created and then we're going to receive the event and now what do we want to do with

Updating Channels in Real Time4:00

So the event name we want to listen to is channel.created and then we're going to receive the event and now what do we want to do with it? Well we want to update our list of channels in the front end. We want that to update in real time so when a new channel is added everybody sees that channel added in the UI. So we can say $this.$wire so accessing the Livewire object directly. We want to grab the channels from the Livewire object and we want to push the event.model. Laravel will broadcast a model event and it will broadcast the contents of the model using the model key so we can push that directly into our channels array. All right back in the front end let's check out if that's actually working. So we can click the plus here

Testing in Two Browsers4:39

can push that directly into our channels array. All right back in the front end let's check out if that's actually working. So we can click the plus here and I'm going to create a new channel called Laracosts. Hit the enter button and sure enough my new channel appears in the UI but you might be thinking that was a page render. Well let's prove it's not by using two browsers side-by-side. Okay back in split mode view let's go ahead and create a new channel called nailed it and you can see that that channel has now appeared in both windows in real time.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟