This episode is for members only

Level up with a premium membership

Join now
Already a member? Sign in to continue
Playing
05. Storing messages

Transcript

00:00
So to store a message, of course, we're going to need a message model and a migration.
00:04
Let's go ahead and do that now quickly. Again, this is going to be pretty straightforward. So we're going to have a message model with a migration. And then if we head over to the Create Messages table,
00:17
we need to relate this to the room that this has been sent in, of course. And we also need to relate this to the user. So let's add in a foreign ID for the user.
00:30
And we'll go ahead and constrain that. We'll do exactly the same thing for the room ID. So let's go ahead and choose that first of all. And then we're just going to have
00:40
the body of the message, which we're going to set to text. So let's just create that as body. OK, let's go and run our migrations here. And now that we've done that, we can go and store a message.
00:54
If we open up the message model, let's first of all set guarded to either false or we'll just guard the ID. So when we create this, we don't need to specify the fillable fields.
01:08
And let's set up some of the really basic relationships here as well, just so we have them for later. So a message is going to have a user. So it's going to belong to a user.
01:18
Let's add a belongs to relationship in here. Hook that up to the user. And then we'll do the same thing for the room as well, so we know that this belongs to a particular room.
01:31
OK, now we've got these two relationships. We can just very easily create this message out in here, assign what we need, and save it. Now, before we do anything, we want to validate.
01:42
So our body, if we just set our rule attribute here, is just going to be required. Feel free to set a max or any other rules that you want. Let's go ahead and run this validate when we submit this.
01:56
And that will validate this and give us back an error message, which we can show in our template. We're not going to do that, because we are preventing the default behavior when
02:05
there's no content. But obviously, we always want to validate on the back end. OK, so let's create our message out in here. There's a couple of ways to do this,
02:14
but I like to just start with a made model without storing it, assisting it in the database. So we just go ahead and make this out using the data from here.
02:25
And the data we've got in here, we can use this only to just grab the body property and put that into there. Now that we've done that, we can just
02:33
go through using the relationships that we've defined to associate each of these values. So let's say message room using that relationship. And we're going to associate the current room that we're in.
02:46
And we know that we've got that at the top here in a property. So we can just directly pass in room. Now, we can do exactly the same thing for the user as well. We can either do that or say user,
02:58
and then use the relationship to make a message. But I much prefer doing it this way. So we're going to associate the currently authenticated user, which we can grab using the auth helper function and user.
03:10
And then once we've attached all the relationships that we need, we can then go ahead and persist that in the database. What we also want to do is reset the body. So let's say this reset and body.
03:24
And that will go ahead and just set the body back to the original value, which I'm just going to go ahead and set here as an empty string, just so we're clear.
03:33
OK, now that we've done this, we should be able to enter in a message, hit Enter, and have that work. Let's just really quickly fix up this new line thing. This was just a mistake I made earlier.
03:44
So what we want to do is say or. So if we're not doing shift or if there's no target value, then we want to prevent the default behavior. So we should now see that just work without going down
03:56
to the next line. OK, now that we've got that, we should be storing some messages. So let's go over to our messages table.
04:02
And there they are. These are specifically for a room and for the user that we have. OK, great, so we can now store some messages.
04:12
Let's go over to the next episode and look at outputting these on the page and having them update when the user who's writing it sent the message.
14 episodes1 hr 23 mins

Course overview

Join a room and start chatting! This course covers building a multi-room text chat app with Livewire using Laravel Reverb for real-time updates.

Using presence channels and client-to-client whispering, we’ll also show who’s online, and who’s currently typing.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Comments

No comments, yet. Be the first to leave a comment.