YouTubevideo· Oct 15, 2024· 1:29:31
This video is all about Laravel Livewire. Ever since I started building applications in Laravel about two years ago, most of them have been built using Laravel Livewire. Even though I knew JavaScript more than PHP when I started building in Laravel, I latched on to Laravel Livewire and using that as my way of building applications. This video is intended as a crash course into the basics of Laravel Livewire, how it works, and how you might start to think about using it when you're building out applications within Laravel.
So whether you're new to programming in general, but you know a little bit of HTML, CSS, and how an application, a web application, Generally works, or you're coming from the JavaScript ecosystem and, you know, view reactors felt, you know, those client side languages. And so you're like, Hey, I'm just curious of how live wire would fit into this. Or maybe, you know, live wire, but you were just looking to get a little bit more of the basic understanding of this is how it works. So this might be some features that you might not know. This crash course is intended for you.
One of the best ways to think about LiveWire is the ability to have powerful dynamic front end UIs without leaving PHP or as Taylor Otwell put it, uh, live wire takes blade to the next level. It's basically what blade should be by default.
And so if you've never used Laravel blade, we're not going to get into the specifics on how blade works, but the front end aspect, the UI aspect, the view layer of live wire is generally just HTML with some extra necessities, extra magic. That being said, LiveWire is not a templating language in and of itself. It is interconnected with Laravel Blade, which is the templating language.
So LiveWire doesn't have a preference on how you should loop through items because Blade does that for you. And LiveWire just connects or hooks into Blade. So while this crash course isn't meant to be a blade crash course on how to use directives and magic helpers within Laravel Blade because Livewire is so interconnected, we're going to be learning some of those along the way.
This crash course is kindly sponsored by MailTrap, an email delivery platform where
Now let's start with getting a Laravel application set up so that we can use Laravel Livewire for building our dynamic front ends. And what I mean by that is we'll install Laravel Livewire into a Laravel application. Now, if you've used the Laravel new command before, you'll be able to enter the name of your project and then also choose a starter kit. And this is a great way to get Laravel Livewire up and running within an application because the Laravel Breeze Starter Kit allows you to use Livewire.
We're going to assume that you don't have a starter kit installed. And we'll just start with what I have here, which is a brand new blank Laravel application without a starter kit. Now, if you're using Laravel herd for your local PHP management, you can click this plus button up here, choose a new Laravel project, and also install breeze that way, which has live wire already pre installed for you in the starter kit functionality, but I don't have anything installed in this Laravel application.
This is a blink application, no starter kit, no Laravel Livewire installed. So let's go ahead and install it. We can do that by composer require Livewire slash Livewire. And we'll just go ahead and hit enter. And that's installing right now. And the good thing is once this installs, we don't have anything else to do for setup. We're just up and ready to go. So let's go ahead and open our code editor. So since this is a blank layer of application, starting from scratch, we can go into our routes and our web. php file.
And this is the only place we're going to touch for right now. We're just going to get something up and running in our welcome views. So you can see here, this route gets the slash parameter. And then we're returning the view of welcome. And that can be found in resources, views, and welcome. And since there's a lot of stuff in here that we don't necessarily need, I'm just going to go ahead and clear it and I'm going to generate some HTML5. scaffolding for us. And we're just going to call this live wire crash course. There we go.
Why don't we go ahead and say an H one of hello world here, just so we know that we have something up and running. And if we were to view this in our browser, we can refresh hello world. Back in our code editor, we're going to close the sidebar for now. I'm going to open up the terminal and here we can start creating a new LiveWire component. That is the php artisan command make LiveWire.
Now, if you're unfamiliar with the term component, in the structure of a Laravel and LiveWire application, it just means the logic and view, what the person is seeing, what your user is seeing, is all wrapped up into one tidy piece of code. In this case, instead of having all of that on our welcome. blade. php file, we could start by having a syntax of live wire colon, and then our component name.
And what this is doing is inserting that component, that logic and view and everything structured within that component. Into this particular file, if you're coming from the react viewers felt ecosystems or anything like that within JavaScript, you are probably more familiar with what a component might look like. It's just a piece of code that's been squished down into one tidy place to keep it all. So PHP artisan make live wire, and we're just going to call it first component.
Okay, congratulations. You've created your first live wire component. You can see here that there are two files that have been generated an app slash live wire slash first component PHP And a resources views, live wire, first component dot blade dot PHP. So if we were to change this from our component to first dash component, save that, and then the sidebar, we have those two files.
We have app live wire, first component, and then resources views, live wire, first component. Let's go ahead and bring these side by side real quick. So while both of these files are one component, they are both the first component pieces of the first component component. The left side is going to be where we store the logic of our live wire component. And the right side is where we store the view structure, what people are actually seeing on the page.
So without doing any changes to our application, we could say, h1 hello world here, go back to our browser and then refresh and we're seeing the exact same thing because we had an h1 with hello world in our welcome. blade. php file. But now So, we change that to show the live wire first component and that's what we're seeing right here.
We haven't touched this side yet, but that's the basics of what is being displayed on the page. A. I Component that we make with the PHP artisan make live wire structure automatically generates two files, the logic and the view structure.
Now without confusing you too much, we're only going to be talking about the traditional LiveWire component structure, where we have our php file on the left and we'll have our view file here to have these two pieces separated. Laravel LiveWire does have functionality called volts. This is a separate package that you can install using the composer require live wire slash volt command. But once you have it installed, you can make a volt component in the same way with make volt.
And we could call this something like a, a volt component. I'm passing in the dash dash class structure to just say, I want this to be class based. Again, this is not necessary to know. I'm just saying this is a different way of writing it where now the volt component is going to be exactly the same thing that we saw previously, but just both of those files are squished into one file. Go ahead and close that.
Next, let's take a look at how state is passed from the server to the client within Laravel when I'm talking about state within Laravel LiveWire specifically, I'm talking about how do we set something that is stored on the server, but then we can see it on the client. For example, this hello world text that we're sending from the server, we're rendering from the server onto the client. Uh, it's not dynamic in any sense. I can't say I want to change this text.
It's hard coded into that view file that I would have to change at some point. So how can we set it up so that it can be ready for us to change on the UI here, or maybe we can pull it from the database, for example, and set it up in that way. If you're familiar with JavaScript at all, or really any other programming language, you might be familiar with something like using the term let. Uh, counter equals zero. And now we have that defined in our state.
So now I can say, uh, counter equals three. And now if I called the counter variable, we have three. How can we do this within the Laravel Livewire so we can set a counter variable, for example, and be able to display that on the page? Well, state is defined in our logic within LiveWire. So within this class of first component, why don't we go ahead and first, why don't we rename this just so it's a little bit less confusing.
So what I'm going to do is I'm actually just going to delete the first component dot PHP. And I'm also going to delete the first component dot blade dot PHP. And I'll just delete that bolt component that I had made as well. And so now we'll go ahead and say, PHP artisan, make live wire. And we'll call this counter. No, and we're not going to create a test file. So now we have the counter. And counter.
And we're just going to call this counter. And then in here, we'll open up the counter. php. That's in the app live wire directory. And then here we'll open up the counter. blade. php just to make sure we have everything ready. H1, hello world, H1 and back in our browser. Refresh. Everything looks exactly the same. Okay, how can we set this state?
So in our class of counter we can declare a specific state that we want to store keep in state on the server. And then we can also display that in our live wire view component. In this case, the counter dot blade dot PHP. So we can say public string of account maybe, and we can even set that to three. And in fact, here, this isn't really a string, it's an integer. So we can say public.
And then we have access to that state in our blade. php file, and this is blade specific, but you can use the double curly brackets to say, uh, the specific variable in this case count, it's automatically passed into our view, so we don't have to do anything else. It just works. So if we were to show that here on the page, we see refresh. Three. Great.
In fact, why don't I remove this and we'll just go ahead and refresh and bump this up for us to see. Okay, now this state is stored on the server. If I was to change it here to 10, go back, refresh, we see 10. So how can we start to modify this, to adjust this or allow our users to adjust it? Well, this is where we can get into LiveWire methods. How can we make this count different?
How can we increase it, or maybe decrease it, or maybe just change it to something different on the page without having to adjust it here? Well, let's go ahead and close that sidebar. Let's make this happen. So in PHP and Livewire specifically, we can create a method to be able to be run on the page.
So if I was to say, uh, public function and maybe we want to call this, um, change count, and here I'll just go ahead and say, uh, this count is. Three, maybe that would be good. So we're going to change it from 10 to three. So how can I run this method on the server?
This is where LiveWire has these magic ability to be able to say, okay, any public function that we create here in this class, I can actually run it on the client. I can in the UI, click a button and run a specific method. So why don't we go set that up? We'll put this into a paragraph tag just for now. There we go.
And we'll go ahead and have a button and we can just say, change count. And why don't we set the button? And this is where we can call specific methods within LiveWire using the wire colon click command. What this is saying is when we click on whatever this attribute wraps, in this case, a button, the attribute was on the button.
Wire click, I can just say equals change count. Okay. Now I have this button that changes the count, or at least when we click on the button with wire click, we can change the count here, which is going to run this function to change the count to three. Let's go try it out, see what happens. So refresh, we have 10 because that's what we set here on the server. Change count.
It automatically gets changed to 3. We didn't do anything in our client. All we're doing is interacting from our client side to our server side and saying, I want this to happen when this button is clicked. So what's actually happening behind the scenes. You see, when I refresh. Okay. The server state refreshes as well. So it's 10, three, I refresh it changes back to 10 because we're not actually storing this anywhere else.
It lives in the life cycle of the component. So the next person who accesses this page is going to see 10 until they click the button. So how does this work? Well, if we were to take a look at the console, what we can see here is in the network tab, everything that happens is. HTML over the wire. Let's go ahead and refresh. We have this state of 10 files to inspect that a little bit deeper. We have this div wire snapshot over here.
we have a bunch of different stuff that LiveWire is doing for us behind the scenes that we don't have to think about it, but it's helpful to know what actually is happening, especially when you think of security down the line. So what's happening within LiveWire is it's setting up this wire snapshot with all of this stuff. A data, a memo, uh, Git, children, stuff that We don't really want to think about, and then we have this wire effects, wire ID. All of this is wrapped up into this component.
You can see here, there's this div, which is this div. It's wrapped up into this component that we have, and then we have our wire click change clamp count. So in our network, if we were to click the change count button, we see this update method. This is sending HTML over the wire. In this case, it's just an Ajax request. If you're familiar with that, we're just sending specific information to the server and getting specific information back.
So if you take a look at the payload of what's being sent back and in this request, we have this component with this call of method change, count a snapshot. And in that call, we say we want to change count. It's sending that method to the server saying, I want to run this. And then in response, what it's returning is this components array, snapshots, effects.
And, but here's, this is the piece that I wanted to look at HTML div wire ID. And then just HTML being sent back in this case, the whole component is being sent back. But what LiveWire is doing behind the scenes is it's just changing out this variable because it has this a wire ID here in this snapshot of data count memo ID you saw here in this element.
So we have this wire ID. What it's doing behind the scenes, and this is just so you can know, be familiar with what's happening within Laravel and LiveWire, is when we refresh this page, we get this specific ID. So that when we click a change count button, LiveWire knows that I'm going to find that ID and change the HTML that has actually been changed or needs to be changed. And that's what's happening every time we call a method within LiveWire.
In this case, a button has wire click on it that we're sending and saying, I want to change count. So we have state that's been set on the server.
We have a method that has been created on the server that we are calling in our view and allowing our user to change, to click on and change, without having to set up any sort of APIs or without having to set up any sort of routes or controllers or anything like that, we have the ability to interact with our server. and have that update what is displayed on the page.
Why it's helpful to know how LiveWire works behind the scenes in the case of sending HTML or sending requests to the server and then receiving HTML back is that's not necessarily protected in the case that, uh, if I was to, you know, use some JavaScript to say LiveWire dot first dot set, and this is just basically loop hooking into the LiveWire commands and say, I want to set the count to five. Okay. Well, we can do that.
Of course, it's just changing on this specific component, but we have to be aware of the things that we can do, or in the case, the things our users can do. So while state can be set on the server and can be changed on the client with using buttons and wire clicks and stuff like that, it doesn't mean that users can't be malicious and change it as well.
And since we're just calling methods within our front end and we have the ability to have specific parameters, or in this case, arguments that can be sent into this function as well, we can do just that. So if I was to say, uh, change count and we want to pass in the number eight, for example, we could say that this would be an integer width. the number variable and then pass this as number.
And this is just saying now when I change count, I want it to be changed to whatever is put into here. Refresh, change count. Now it's just changed to eight. And this has the same functionality that you should be aware of where we can change this to whatever we want. We can say that this should be 300. And now if I click the button, it's automatically 300. All we're doing is just clicking this button, which has this specific parameter. But what if we did want the user to change that?
Let's say we want them to enter a number and change it to that specific number. This is where we can get into attaching or hooking between the state and the user. view or the UI layer here. We already have the count that we're displaying here. But what if we want to change that?
Maybe we have this input and we want to say, okay, in this input here, I want to have a number and we'll have a type equals number. So in this input here, I want to have a specific number. And when I input it and click change count, I want this number to change to that specific number. Hmm, how can we do this?
Well, Livewire has the ability to what's called model something from the front end and hook it up to whatever the state is on the back end. That's this wire model directive. So we can say wire model equals count. And now, what's happening is anytime we change this input, it's going to change it on the server.
But not necessarily, because wire model is only going to change or push that information to the server. when something happens in this case, when a form is submitted, for example, but we could do something even different. We could say wire model dot live and almost any wire colon blank directives have the ability to have these additional attributes, wire click, wire model, those sorts of things. It's all in the live wire documentation.
So be sure to check into that this is just a high level overview, a crash course of the things you can do within Laravel LiveWire. So wire model live. Now what would happen here is if I was to refresh this, it's automatically synced and I'm going to change it to one. Okay. Maybe a hundred and maybe 35 can see here. If I, uh, start typing a bunch, can't assign this property cause it's not a type of event, but if I was to refresh and maybe like. Type a bunch.
You can see that it doesn't necessarily reflect right away. There's some lag. It's because it's automatically debounced for us. What that means, it's not just trying to send a bunch of requests to the server right away, because anytime we do something within LiveWire, a wire click or a wire model live, for example, it's going to send a network request, a submission to the server. We just sent three there. bunch there.
So there's ways that we can stop that from happening. But for now, what we're going to do is we're just going to remove the live. And then what we want to happen is we want to, and maybe we don't even call this count. Maybe we want a public end of a number and we can just set that. We don't even have to set a specific. variable for it. And we can change this to number. And here we want to change the count to whatever is in that number.
So we can go and say number. And then if we were to go back into our page, we'll go ahead and say, maybe we three change count and nothing happens because we can see here that we have this wire model. It's attached to the number. But when we change count, uh, we're not changing that number. So what we can do here is wire model has wire model live, debounce, blur, and change.
And in this case, blur is probably going to be the best option when a user clicks away from an input, change the count or the state on the server. So if I was to go back here, what we want is wire model dot blur. And what's going to happen here is when we click out of the input, And click on the button in that case, because we're removing our focus from the input.
Then the number is going to change on the server and allow us to pass that into the change count method. So refresh, let's say seven. Change count, seven. Okay, maybe 14. Change count, 14. And now we have this ability to, uh, when the input is changed, let's go into the network. We have 20 and click out.
We're sending the update to say, I want to change this. I want to change the count to 20. Change it and 35 change. So now that we know how to call specific methods from the front end and, push data to the back end to receive information back, we know how to set state and also how to change that state on the front end.
How do we put all this together to maybe say, have a form where the user can submit information, and then we can figure out what we want to do with that information on the server. Well, we kind of already know Halfway there. So why don't we take the extra next step to do that? Let's create a form so that we can put it all together and maybe save some data to the database.
Building forms within LiveWire is already extremely easy, but you also have the ability to replicate those forms across different components by extracting the information out into its own form component. So while we're only diving into, again, a crash course level of here's how to build a form within LiveWire, You can take this one step further and read the documentation on how to build forms and extract abstract forms into different components, but using and submitting to the same form.
So that way you only have one place where you're changing the logic for a form that might be used in four or five different components. But how do we take what we learned with this? piece of information and build out a form to, let's say, submit data that the user is inputting into the database. Going back to our welcome. blade. php, where we have the live wire counter, why don't we go ahead and create a new component?
And why don't we make this component a way to store into the database, how many times we or how many birds we might see on any given day. So think of this as like a bird entry journal, maybe like a bird watching type of journal. And then we're just going to call this PHP, uh, artisan make live wire. And we can say, bird count or maybe bird form. That might be a good way of putting it. Okay.
So we have a bird form and a bird form dot blade dot PHP. And we'll just go ahead and enter that here bird form. So we can put that over here and then we'll go ahead and pull this up here. Okay. Now what we're going to want to do is have a. Simple form set up for us. So that whenever we maybe, uh, you know, go out through the day and we come back home and we say, I saw eight birds today, maybe we can take some notes on how many birds we saw. So let's think about it.
We have the ability to maybe input, and then we have, uh, maybe like a text area. And then we'll also have a button and this can be add a new bird count entry. There we go. And let's wrap this all in a form.
And now let's go ahead and set up our state on the left side. From what we've already learned, we're just putting together this state over here in our component, our PHP logic, but then we're just going to attach it or hook it up to our form itself. And we'll learn some neat things about how LiveWire deals with forms. But for now, we'll just go ahead and say a public string.
And we'll say that this is a Maybe some notes and we can put it in this text area and above that we'll put public int of count. So how many birds did you see? And then that should be pretty good, but now we need to submit functions. So maybe we'll say public function, submit, And we'll create a new entry. So that's the model that I created behind the scenes. I created a new model.
It just has a bird count and then a notes area. So we'll go ahead and say entry, and we'll say create, and here's where we can say, uh, we'll say, I believe I said bird count equals this count, and then notes is notes, this notes. We'll go ahead and wire model these up and then within live wire itself, we can say wire submits in the form handler.
So that way, any type of button for this form will automatically get use this method that we're submitting. So the submit method says we're going to create a new entry, uh, bird count and notes. Let me make sure that I have that set up properly. So we have bird count and notes. Perfect. And let's see what this looks like. So we'll go ahead and refresh. I don't know why that's looking weird.
Ah, it's because, uh, the text area needs to be a full component. There we go. Text area. That should be perfect now. Slash text area. There we go. Okay. So now we have a count. We have a text area. We have a button. This doesn't or isn't supposed to look good. Uh, but why don't we go ahead and just leave it how it is for now?
Or maybe we'll just add, uh, maybe some div with a label and we'll close that off and add another div with a label of notes and close that off. Let's see what that looks like. That's a little bit better. Okay. So we have bird count and let's change this.
to a type of number and then we have our text area with notes so we can say we want, maybe we saw three bird counts, three birds for today and we have some notes that we saw a robin. Let's add a new bird count. And nothing happened. Well, let's go ahead and check our database to see if something actually did happen. I'm using Laravel herd and it has actions for table. Plus I can open that up in table.
Plus, and it looks like we have an entries table and we have a new entry. Robin with a bird count of three. Now there's some things that didn't happen within this form that usually you probably would want to happen as a user, you would want. these fields to be cleared. There's a bunch of different ways that we can do this within LiveWire. For example, we could say, uh, this reset, that would be one way of doing it.
So if I set a new bird count of five and I saw a blue jay, then the form itself has now been reset. So this reset is one way to do that. Uh, we could reset each individual field if we wanted to. Or we could pull the data as we're pushing this into entry create, for example, it could be this poll count and this poll notes. And that was, and what's that's doing within live wire saying, I want to pull that data.
And essentially reset the field as we're pulling that data to push into an entry or the cool thing within LiveWire and how it pushes data into your database is if we use the same naming schema that our database is using, for example, if I was to change this to a bird count Going to call it bird count bird count. I believe we can actually use camel case as well.
Let me see if this works. And then in our create what we can say is this pull and what will that do is just pull all the data that we have in our state that we're submitting to the form. within here. Let's see what that works. So refresh. We have a bird count of 25. We saw a cardinal and it looks like it can't be, it needs to be the, uh, the bird count.
This should be the correct way because it needs to be the same, uh, variable that we're using in our database. So bird count, Refresh 25 cardinal and add new bird count. And yes, because we are using, let's go ahead and refresh table plus because we are using the bird count integer that we're pushing into the database. This works just fine for this poll. So a little bit easier way to clean it up.
If you want to use the same Variable names that you have in your database. For me, I prefer to stick with a camel case, for example. It just means here we would want to create the entry, and then reset the form instead of pulling the variables. Again, it kind of comes down to personal preference, how you would To reset those values within a form. So the form has been submitted.
How do we display information within LiveWire and maybe loop over things in LiveWire to modify then, because we know how to run methods on the server. If we wanted to change something, maybe we know we can kind of piece together. Okay. Now that I can interact with the server and the database, I can delete specific bird count entries. But how do I even have them displayed? In the first place, let's take a look.
So we've kind of glossed over whenever we have created a new live wire component, this whole public function render method, we haven't touched it. Well, what this render method does is it pushes information into the view. In fact, we've kept the same name in this case, bird form and live wire dot bird form. We've kept it as the same naming convention throughout, and we haven't changed any of it. You can pass in a different view.
For example, we can omit this and everything is going to work exactly the same as we would expect because live wire works in that way. It's expecting that you probably have a view similar to this. But the reason why we're not going to emit it and not going to delete it is we can pass additional information to the view. This is work similar to how you would use a controller.
And if you're not familiar with that within Laravel, you have information that you can say, instead of creating a state here, maybe I want to just have information here passing in things like, um, maybe like state. And I could just say, or we'll just say a message for example, and I can say hello world, and we're passing that into the view.
Well now, here I could echo out or display the message that I'm passing in. Hello world. So, you can think of it as state that is being set in a different way, but instead of setting it on the component, What we can do is pass it to the component.
And that's the way that I've thought through all of this and it works best in my head of anything that we have in the render method is stuff that we are perhaps pulling from the database and pushing it to the component. And anytime the component re renders, in this case, if I was to click this button, the message is going to be pushed back into the component. It changes and re renders every single time something happens within the component.
And this is going to be useful for us if we're adding something to the database. And in this case, bringing something from the database and pushing it into the component. So what I want to do here instead of message is push all of our entries, the bird entries forms that we have created in our form. I want to display those on the page so I can go like this entries, entry all.
And then here on the side, we're just going to create a Let's just go ahead and echo out the number of entries, the bird count, and then maybe we also want to echo out the notes as well. Refresh the page here.
We have 3, Robin, 5, Blue Jade, 25, Cardinal. So if I was to create a new bird count, let's say, uh, I saw one bird today and it was tiny. I know that's not a bird. species or a breed, whatever. What do you call birds? Bird breed? Add a new bird. It automatically gets added. Why is that? Well, because we are rendering that information.
We're passing that information to the front end. So anytime we call a method, in this case, submit through this wire submit, the component is refreshing. And it's grabbing new entries and pushing them to the page. There are also additional ways to get information from your server to the page that you're not setting in state and not necessarily pushing to the component through the render piece. And that would be the mount methods.
Why don't we take a look at what that is used for and maybe some things that you might use that over the render method. So you could think of the mount method. If you're used to PHP and have built anything in PHP, there's this sense of a constructor, which takes an outside parameters and initializes the object's state. And since there is no constructor within a LiveWire component, you have the mount method for accepting those parameters and then initializing the state of your component.
So again, how is this different from the render? Well, we're not essentially saying, okay. Within this render, I want to take the notes and, uh, change the notes to a specific note from a specific entry. That's not the way to do it. You wouldn't push that to the component. Again, think of render as being pushed To the component, push to the front end to be visible on the component.
And then we're going to think of public function mount to say, I want to set the component before it's pushed. In this case, uh, I want to say this bird count is three. Now, of course we could set that up here, but let's say we're accepting parameters. from our component method.
In this case, maybe, um, this needs to be dynamic and we can't set anything dynamic up here before the component is actually rendered. So, what we could do here is information that is passed to the component would then be accepted in the mount and passed to our state before it's rendered. I know this can be diving into the weeds a little bit, but stick with me, it will all become a little bit more clear.
Because if we have this bird count, refreshing the page over here, we have a bird count of three. But now, what if we want to pass information into our component? Again, there's two ways of looking at this. The state is what is being created. created when the component is initialized, the render is being pushed. So in this case, we're grabbing stuff from the database and pushing it to the front end.
And the mount is going to be accepting things. So we have our welcome dot blade dot PHP. What if this was dynamic? Maybe Maybe we want to say, okay, this bird form. For some reason or whatever we want to pass and say, uh, this should automatically have a bird count of three. Okay. Well, how would we accept this as a live parameter in our component?
And so this is just a parameter, that is being passed to our component because now this is dynamic. This number even could be dynamic as we are. rendering this component. We could have multiple different bird forms where each of them have a set number, or maybe we want to set a specific date for that. Those bird forms are specific notes. The specific example doesn't matter as much as grasping the example of what we're trying to show.
So in our bird form, we can accept specific parameters, let's say a bird count, and then initialize the state with the bird count. So, what we're doing here is, this bird count is being set as 3. The mount, well we couldn't necessarily do this in render because we can't push information that we haven't first accepted. The mount is saying I want to collect that bird count that is being pushed into the component, I want to accept that.
Set these states. To that bird count has been accepted from the component itself, and then finally just rendering that to it. So we should have a different bird count shown. So refresh three. There we go. If this bird count was emitted, the form would not work as expected because this mount is saying, I need something to set the bird count.
So we have this, uh, unable to resolve dependency error because it wasn't able to set that state. It's trying to retreat something that isn't exactly there. So when would you use mount? When you know that you're accepting specific information that the component needs to be initialized with. When would you use render? When you're getting information and pushing it to the front page. So we're going to delete the mount for now just because it doesn't necessarily need to work for this particular form. Refresh.
Let's add another bird. Uh, we see three birds today and we're They were all beautiful. Perfect. That's how simple forms can be within Livewire. How can we validate these inputs now? Well, we can validate them within the submit function. We can say, uh, this validates and we can go ahead and pass parameters here.
We can say this is a required, it's an integer, notes required, and it's a string, and then anything that is incorrect within here would be then passed to what Laravel calls a message bag, where we could say, uh, if there are any errors, then we just want to show them here because, uh, Laravel has the ability to have specific errors being sent to the front end.
And because Livewire and Laravel Blade are so interconnected, It just works like this. So if I was to type in, I don't know, E and have these notes, well, first we have client side validation, but we, the notes are required now too. So a new bird entry, the notes field is required and we didn't set any of that up client side.
For example, we don't have this, uh, this text area as required in this form, but we are displaying that error that is being passed from this validate field. Another thing specifically within LiveWire 3 that you can do as well is there are so many things within the LiveWire documentation that use attributes within PHP.
So instead of having specific validation methods or specific validation rules within your methods, you can then instead use a validation attributes. And here's how that would look instead of, uh, the validation here. Let's go ahead and replicate that. We would say, I want to validate, let's go pull this in. Use livewire attributes. Validate, validate, required. And we can say that this is an integer.
And then we want to do the same thing here, validate required string. So now we can just call the validate method. And this is just going to say, now we have it set for all of our fields, no matter what method we're calling. So instead of having to replicate the, this validate piece for any additional method that we have on the server, In this case, maybe we had multiple functions that we're calling that we need to validate these same fields.
Well now we can just co locate those to the field itself instead of the method. So this validates and everything is going to work exactly how we would expect. So three, no notes, notes field is required. Nice. So we have a form that we have validation on and we can pass information from the server to the client with information that we just stored in the database.
And because live wire components refresh, anytime we run a method, we have this neat functionality of not having to refresh the page, but still have this information reloaded because we're refreshing the component. So now how can components talk to each other? Because this component is just all in one piece. But what if we needed to, let's say, refresh this list anytime we submitted a form that was in a completely different component?
Well, you can use events within LiveWire. So how do two components and layer of a live wire talk to each other in this case, maybe in component one It has a specific count. Well, how do we tell component two? What component ones count is like, how do we say?
Okay, when this count gets updated Let's make sure this stays in sync Well, let's think about it for a little bit First, I guess you could say we need to make sure the database stays in sync, but then how do we make sure the database, um, stays updated for the page? Well, Luckily, LiveWire makes this incredibly easy. There's these things called events.
Think of it as like a text messages anytime you do something, you send a text, someone else is notified of that text, and then they can. Read if I sent a text to someone, someone else's phone gets that text and they are then notified to read that text.
Well, in events in LiveWire, I guess it's kind of similar in a way where you can say, okay, when component one's count changes, I want to send a message and on component two, I want to then listen for that message. And depending on what that message is, maybe I can then. Uh, listen, and then refresh the account, or even just refresh the component. And this is what events is in a nutshell.
One component says something. Component two is listening for it and says, okay, I received that event. I receive that. And now I'm doing something with it. So. Let's write some code. Let's go ahead and start with a brand new component. In our welcome view, we'll go ahead and remove this and we'll call this PHP artisan, make live wire, uh, send event.
And then we'll create a second component, PHP artisan, make live wire, receive event. Awesome. And now let's go ahead and close this out.
The thing we want to do is let's do the send event first and here in welcome, we'll go ahead and say, uh, send events, live wire, send event, and maybe we can wrap this in a div. And then we want to receive an event, so receive event. There we go. We'll say an H1 of let's send an event.
Let's pull up send event dot blade dot or send event PHP here and then see what this looks like. Okay. Send event. Receive event. Perfect. So in our send event, we'll make this, uh, H three, let's do that just to make it a little bit easier. Uh, what we want to do is now talk to these two components because they're in different files. So, in our welcome. blade. php, these are two live wire components.
We already talked about and got to see if I was maybe in one component, um, in the send event component, if I was to click a button, for example. of, uh, let's just say maybe we want to send a message, wire click of send message. Then we already know how to, you know, trigger that here and then display a message. But how do we do that across components?
Because they're, they're, they're their own entities. How do we make sure that those components know that they, one, each other exists. And then how do we actually. Um, have things run. So let's go ahead and send a message. And this is the wording that LiveWire uses is specifically in LiveWire v3 is dispatch. So we can dispatch this message in this case, this parameter message, or this name dispatched event of message sent.
And then we want to say or send a parameter of hello world. Okay. Well, actually, in fact, let's go ahead and remove the parameter itself. We just want to dispatch a message sent. So now in our receive message, receive event, let's put that over here. So in our receive event, receive event, we need a on message sent attribute. What this is saying is when.
this component, listens, it's listening for a specific event. In this case, the message sent named event, then we can do something with it. In this case, display, message, message. And why don't we bring in the attributes. So the live wire attributes on, and we have a public string of message. So it's immediately set to no message yet, but when we receive that message, we can change the message.
Uh, so we can say something like, Let's receive an event. Message. Perfect. Let's go to our browser. Refresh. Okay, let's send an event. We have a button to send an event. No message received yet. Again, these are two different components. Send message. Unable to resolve dependency. Let's take a look. Aha. We do not need a message. This message equals we didn't pass in a parameter and so we're not going to be receiving a parameter just yet.
We're getting ahead of ourselves a little bit. So this message equals hello from the other component. Perfect. Refresh. Send a message. Hello from the other component. Okay. Let's think of what we just did here from a practical standpoint. We have two separate components, in this case, in our welcome. blade. php, there are two separate components. They don't know about each other.
The only thing they know about in receiveEvent is this little onMessageSent attribute. That's all it knows. So we have this incredible flexibility and power within our components where let's say maybe we have one form. But when we submit that form, we need to tell another component to do something, maybe talk to the database or, maybe collect some more information or just as simple as refreshing a component.
And that can all happen here. So, for example. Maybe we want to, um, like refresh the state, and this is a poor way of showing an example of what you would do in a real world application. But let's say, for example, that now I want to change this back to what it was.
For example, maybe we want to, uh, public function reset component, and we want to say this dispatch reset component so that here we can reset the component on reset. this reset. So in our send event blade, maybe we can have a second button that says let's reset the component. Why don't we give it a try?
So sendMessage, hello from the other component, ResetComponent, no message yet. And we can keep doing this as much as we like. And these are two components that are talking to each other. One is resetting the other. Now we kind of already got a glimpse of how we can also send parameters to, for example, in display message, maybe we also want to set the message itself. So we can say, Uh, the message should be, maybe we'll say new message.
We can call that new message here so that in our send message, we can actually send a new message. And maybe we can even do something fancy of this message, public string message. And then in our send event blade dot PHP, we can have an input that's tied to the message. Wire model, we can say dot blur here to make sure we, save the message before we're actually sending it.
So let's go ahead. Now we have this place to put a message. Hello from the other component. Send message. And how are you send message, reset component. Perfect. So this is just another way where we can, instead of just saying, Hey, listen for something and now do something we can say, listen for this. And by the way, here's some additional information that you might need.
This is extremely high level overview. Events are so powerful. And I would say, Okay. So let's take a look at the documentation for all events. One thing to note in addition to everything we've kind of talked about is events can be dispatched to specific components as well. So in send event, instead of just this dispatch, we could say to, and then have it go to a specific component.
So in this case, receive event that way, if you have multiple components on the page that are listening for maybe the same component, you only want to send the message to one of those components. That would be one way to do it. In addition, everything that we've talked about, including events and everything before it can all be called from from JavaScript as well.
So for example, reset component, we can reset this particular component, the send event component, uh, by using this dollar sign refresh and what this will do. is refreshed the specific component that we're on.
But this handy dollar sign can do a whole bunch of different stuff from being able to call dispatch methods from our JavaScript, in this case, Alpine, or being able to call specific parent components to send events as well.
Last, but certainly not least, how can we use some single page application type of functionality within LiveWire v3 that we have the option to add things like loading indicators or being able to navigate to a certain page without actually refreshing and loading the page. Well, here's how you can do that within LiveWire. Let's start off by clearing this out and we're going to create a new LiveWire component again, make LiveWire. And we'll just call this lazy.
And you'll see why we call it lazy here in a little bit. So let's go ahead and pull up lazy. blade. php and here we'll say live wire lazy. Now in our lazy blade, let's say we have a component that. a bunch of information. Maybe we're querying a third party API, or maybe we just have a bunch of information in our database that we want to display on the page.
Well, let's say we, uh, in our public function mount component, we kind of just fake how expensive that query might be. We can say that this sleeps for three seconds. And so what's going to happen is when this component tries to load. The page is going to stall, and we can say, uh, Hello there, world. So, in our Crash course, we refresh. You can see that the page is trying to load. Refresh.
And now we see hello the world. Let's just go ahead and say, this is a date now. So refresh. One, two, three. Ah, I don't believe it's date now. Let's see. Just now, there we go. So refresh 1, 2, 3, and now we have the date and you can see that when we refresh the mount is, for example, if we are.
We're kind of replicating an expensive query that might be happening. Well, how can we make this better for our users? If you were in a real world application, most of the time you probably want them to see the rest of the page. For example, we might have additional information on this page in the welcome. Maybe we have, uh, here's some additional information for you. Well, they're not going to see that information until the page fully loads because every component needs to be loaded.
On the page. Well, there's an easy way to do that within live wire V three. So we have some additional information for you here, and then I'm just going to say lazy for this component, but this means this component is going to, uh, not load until everything that it, it needs to load. In this case, sleeping for three seconds actually happens. So refreshing the page. Here's some additional information you have.
And then we have the component load. Now you might say, ah, that's a little boring. Like what if I need to show some information or maybe just some placeholders text until that comes in. And the good thing is we can do that within lazy loading. You can render placeholder HTML. So we're just going to go ahead and copy this public function. Placeholder. And here's where you can put in maybe like a loading spinner.
So we're going to go to lazy and we're going to put the placeholder here and here's where we can put in a loading spinner, but it's any kind of HTML that you might have. So I'm just going to say, maybe like a paragraph of loading critical information. So when we go back to our page, we refresh loading critical information. And then we have our component.
So you can see how this can be used in conjunction with a bunch of other stuff that we've used of, of maybe we're passing information. And instead of waiting for all of that to come in at once, now we can just display a skeleton, or maybe we can break this component down to only the pieces that we're going to be having to wait a little bit for.
But this can be an extremely beneficial thing for your users where you can make it seem like a, a modern application while using server rendered content. We didn't have to write hardly any code to get what we're seeing here. As we start to wrap up this crash course, let's go back to our form component, where we're loading a list of bird entries that we created. How can we make this better?
One, to just look a little nicer with some niceties that LiveWire gives, but then how can we start to learn additional things from there? So in our welcome. blade. php, let's go ahead and bring back our live wire bird form. And then in our browser, we should see that here. How can we add some parameters to our method to make sure that we're deleting the proper One.
So maybe we want to add like a X or delete button next to some of these so that we can delete them. Well, let's go into our bird form and then we're going to open up the bird form over here for our blade. php. As we go through here, maybe we have a div in each one of those to loop this in. And then what we want to do is maybe have this, uh, button that's just says delete. And what we can do here is we can pass in attributes to our delete method.
We already talked about passing this in when we showed that you can, modify this Okay. this from the front end, but now we can also pass in things that we're looping through. So over here, we can create a new delete method where we can say, uh, I want to find the specific ID that we're passing in because we're looping through them. And then I want to delete this. Okay. This is all well and good, but there's something to note here. There's an issue. And we might not see it specifically on this page.
If I was to start deleting things, uh, we might not be deleting the proper ones. And eventually we could have some errors. We didn't have one here, but eventually we could have some errors when we start deleting things because we're not properly looping through the things that we need to delete. We, we need a key within LiveWire to say. I want to keep track of all of the entries just in case anything changes, maybe, you know, things get mixed up or out of place.
I want to make sure we're deleting the proper ones. And what we can do here is we can set a wire key. What this is doing is saying now each one of these has its own unique identifier. So if I was to refresh and look at this and the inspect tool, we can see a key of nine because that's the ID that we have. So now, Each one has its own individual key. So now when we're calling this wire click, it's, it's connected a little bit better.
We're not going to have any issues looping through them. Great. Let's add one more attribute, one more wire colon directive. And there's so many within LiveWire. There's so many things that just make this incredibly easy for us to do simple things within our application, things that you would do for every single application. And again, we're not going to be able to have time to dive into all of them, but here's one more that it's easy to show on this particular form.
While we're not having anything extremely nice happening right now, sadly we could make this a lot nicer. We could add some CSS and make this beautiful, but that's really not the point of this video. But one thing we can do is we can say wire transition. Okay. That's all. Yes. That's all. Now, anytime a new, entry is added to this form, it's going to fade in really nicely. Let's go ahead and refresh.
We'll say to Cardinal, a new bird entry forward. Ooh, see that we had this little, transition there where we can say, uh, one, and this is a blue J. Bam. All of a sudden we have this nicety added for us out of the box without having to do anything. Let's see how many characters it was. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 characters. And we have that given to us out of the box.
Let's do a two for one here. LiveWire gives you the ability to have a full page component. In this case, we've been using components kind of put into our welcome. blade. php. Well, instead we can route a request in this case, maybe like slash post or slash users to specific. components, but, uh, we can then just have those be the full page and not have to be kind of confined to this one. Welcome. blade.
php that we've been using. But in addition, when we click on one of those routes, we can have this wire navigate, which essentially fetches the information. So, I'm going to go ahead and create a new Hmm.
Maybe why don't we go ahead and do the bird form counter and lazy and have those be three separate pages. What we can do in our web. php. Let's go ahead and close this out so we'll go ahead and do is we'll do a route of get, and we can say counter, and then we're going to pass in the live wire class. So in this case, it's going to be the counter class.
And then we can do the same thing for the, maybe we'll say Uh, birds and we'll pass in the bird form. And then we'll do the same thing for the lazy class, and we'll bring that in as well. So here's what's going to happen.
We're going to have the ability to use LiveWire as a full page for these individual components, LiveWire gives us the ability to set up a layout file for us so we can say PHP artisan, uh, LiveWire layout, that's going to generate a new components. Uh, directory that has app. blade. php.
Everything's already set up for us to use LiveWire in the same way that we've been doing. But now let's just go ahead and, uh, have a new, maybe a new like nav here where we can direct to counter, to bird, to lazy. And that way, since it's in this. layout component, it will automatically be outside of the components that we have. So again, not using the welcome. blade.
php that we set. That is just standard blade views, but now we have the ability to see all this. So we'll do counter. All of a sudden we have this counter component. bird component, lazy component. You can see here the lazy is, is waiting. Uh, so well, first, why don't we change that lazy component? So for the full page component, uh, it's can do that lazy loading that we already set up for us. There's a couple of ways to do it.
You can do it as a attribute. So for example, we could set a lazy loading by default on the class itself. Or we can set it in the full page lazy loading here. So we're just going to add this lazy at the end. Why don't we do that. In our web routes, we're just going to say that this is lazy. And now, in our page, we should be able to refresh. Loading critical information. Perfect. Bird. Lazy.
Loading critical information. Perfect. I love where this is going. Now we can add the wire navigate option. So, what is happening here? If we inspect, we have the network request. We are doing a full page request when we go to the counter, we're doing a full page request and see here We're getting all this information We have bird we're doing a full page request. We have lazy.
We're doing a full page request How can we make this so it doesn't happen? Well, in our app. blade. php, that's the layout, the live wire layout that we set up. We can say wire navigate and put that for every single time we're directing to a new full page component. Now we're going to refresh, counter, bird, counter, bird, counter bird.
You can see here that we're fetching this information instead of doing a full page refresh. Lazy. It's grabbing that component and essentially replacing the things that we need to replace. And the cool thing is it's happening on the mouse down event. So we're already fetching that information. You see here that counter refresh right there. Like go ahead and clear that out.
When I click on bird, I haven't lifted up the mouse just yet, but we already fetched that information so that when we release the mouse, all of a sudden we have that information ready for us to view. You can see here how this can come into play when you're creating more functional, more, um, maybe where your users are expecting a snappiness. And you get this out of the box within LiveWire 3. Counter. Bird. Lazy.
Speaker 11: We've already learned a lot, but why don't we take things one step further as we close out this crash course in Laravel Livewire. How can we keep our actions secure within our application? How can we make sure that the user is authorized to do X, Y, or Z within Livewire? And this is a perfect opportunity to use the sponsor for this video, MailTrap, to test our emails when we send a notification to our user. The great thing about MailTrap is they have an easy way to set up email testing.
So we can just hook up some SMTP credentials and send these emails and get them to this inbox live on the internet so that we can see them in real time. And, but you can also just change some variables and start sending in production. They even have a demo domain. So you don't have to set up your own domain to start testing in production as well. But why don't we go with email testing? So we're going to click start testing, and then we have these SMTP credentials.
The cool thing is you can actually just pull open this Laravel nine plus right here, and we're just going to copy that. Now you can try to use the username and password that I've created. I've already reset it. So. Don't try, I guess we'll pull up in our dot ENV file and then go ahead and paste it in our mail. Perfect. Now that we've got that set up, why don't we take a look at what the live wire docs mention for security?
We've already talked about this a little bit, but anything that can be called from the front end within LiveWire, something that we've permitted or created on the backend, a function, a method can be adjusted on the front end. So if we're calling some sort of delete function, where we're finding a particular post, for example, and deleting it, Uh, we need to make sure that that person has allowed to delete it because they could just pass in any ID to this parameter and delete someone else's post.
Maybe they're not even seen on their page. So how can we make sure we do this in the proper way? Let's dive into the good. Why don't we go ahead and create a new LiveWire component so that we can get this set up properly. Uh, let's go to our web routes and maybe we want to call this bookmarks maybe. So we can say, Uh, PHP artisan make live wire bookmarks. That looks great. Okay.
So if we go into our bookmarks and then we have our bookmarks blade dot PHP, what we're going to do here is just create kind of a, a simple structure to be able to add new things and delete them. So, since we've already learned this, the ability to create things, I'm just going to skip ahead and get something put on the page for us right now. All right.
I have something scaffolded out for us, which is just a form with a name and a URL, and then a button with submit where we're just saving bookmarks specific to the logged in user. And now we're going to use this auth login using ID function to basically give us and pretend like we have a user already scaffolded out. And the cool thing with Laravel and just out of the box, we already have a user's. Tables. If you take a look at here, we have create users table.
It's one of the migrations, the first migrations that we have. So what we can do then is in our factory, in our cedar, we can just create a new users. If we go into our database cedar, uh, we can say user factory, create name, test user email. And that all looks good. So we can pull up our terminal and we can just say PHP artisan, uh, DB seed. And now we should have one user.
So what we can do in our mount function is call this auth login using ID and just log into the current user. So we should be logged in, in our bookmarks blade. php. We should be able to say like user auth user name and everything should be good to go. So why don't we create a new route so that we can see this particular user.
So we can say route get bookmarks and we'll pull in that right now. Here. And then why don't we go to our bookmarks page and perfect. We see this test user logged in. This is a great way to kind of start thinking about authentication and authorization without having to scaffold out a whole log in and register page. We already have the information we need in our database ready to go. So this is the name of the bookmark and this is the URL.
So why don't we go ahead and call it. We'll just say. Uh, YouTube, youtube. com. We don't have any validation. That's not really what we're worried about right now. I'm going to go ahead and click save and undefined method user bookmarks. Let's see what we have here. Ah, it looks like I forgot to add in the user table and actual bookmarks functions. Why don't we do that? There we go. Let's see if that saves that now.
Looks like we're still getting an error, but we should be good to go. That might just be my code editor. So we'll go ahead and refresh. We'll say YouTube and then youtube. com. Save no such table bookmarks. I forgot to run the migration. We'll go back again PHP artisan migrates. There we go. Third time's a charm. Let's go ahead and see Save and we should have that update.
So why don't we start listing these out then our bookmarks blade. php Why don't we just go ahead and create a new div to start ordering them out? And this is just saying auth user bookmarks as bookmarks, but what we can do is say bookmarks as bookmarks, and then put this into the render function. So we can say bookmarks is auth user bookmarks. There we go. We have YouTube. Perfect.
And now we'll have, uh, Twitter, twitter. com, save, perfect. Looks good. Well, uh, now here's where we're going to create a, we're going to have a delete button here. And maybe first we want to make sure we're doing everything correct. So we can say a div here, and we would probably want this to be like an unordered list. This is just for quick purposes to get this demoed out.
Uh, but we won't, we want to have this be a wire key of bookmark ID. That all looks good while the bookmark URL, and then why don't we have a button with delete. Now, again, here's where we can pass in any ID on the front end, but we want to make sure that no one else can delete it. So for example, why don't we go ahead and create another user just to get this put into our minds of what could happen.
So we can. Go to our database Cedar and why don't I create a new user and we'll say test user to test to, and then I can call PHP artisan DB. See it again. Now we should have two users. So here's what we're going to do. We're going to now log in with our number two again. This is just like nitty gritty way to do this to show this without having to have any sort of authentication set up outside of this.
So now if we refresh, We don't see anyone. We're logged in with test user two. So now if I click, uh, Twitter, HTTPS, twitter. com, and then we also have, uh, YouTube, youtube. com. Let's go back and create our delete function.
And the Copilot already scaffold this out in a proper way. This will just then throw an error if we tried to delete something that is not part of our user. This is saying, I want to find the bookmark that is associated with this user. But for whatever reason, let's say we have a bookmark uh, find, and then we just delete from book. There. So, bookmark find.
Then, delete here. Now, why don't we do this so it's not continuing to yell at me? I'm just going to say bookmark create. There we go. All right. So if we go back here, we have the ability, if we were to inspect this, we could say in this delete button, we want to delete two. Well, it's not going to throw an error. We can delete it. And we deleted the second one.
So if we were to log back in to our first user, we shouldn't, we should only see, yeah, one item. Okay. Now this is a security issue because we didn't want to delete that. Well, Copilot already kind of gave us, um, A sneak peek into what's a good way to stop this. We could say auth user bookmarks, find ID, delete, and this is just saying if it's not associated to this user, it's going to throw an error. It's not an authorization error, but it works.
If I was to inspect this, this is delete one, but I want to delete three. Delete. Then we have this error call to member function, delete on no, there's no bookmark associated with this. Well, that's one way to do it, but let's say either you forget to have this scat of scoped of off user, and this is probably a better way to do it because now you have this security policy and that's what we're looking at. So I'm going to revert this bookmark find.
And we'll log in using number two and let's go ahead and create a policy. We have PHP artisan, make policy, and we can call this a bookmark policy. Actually, I think we'll, we'll let Laravel give us a, some prompts to be able to fill this in. We can call it bookmark policy, and we want it to be associated with the bookmark model. Perfect.
Now we can go to that bookmark policy, and here's where we can say I only want the user to be able to delete their bookmark. So return if bookmark is user. So if the user of the bookmark Is the user who's trying to do it because I have in our bookmark model, this user method that belongs to a user class is going to throw an error, and it's going to have a policy that we have.
Now, how do we tell our live wire method that this is what's happening? Well, let's take a look. Bring this up over here. And then in our bookmarks blade, the only thing that we need to do before we delete is say, we'll say bookmark bookmark, find ID. Then we can say this authorize, and we can pass in the delete method.
Specific for this bookmark and that delete method is on that bookmark policy. We're saying I want to check the delete method specific for whatever we're passing in, in this case, a bookmark, and then we can just say bookmark, delete. So why don't we give this a look in our browser, refresh. We're on, we're logged into user two. Let's inspect this. I believe the first item is still available for us.
I can type in one and delete and return value must be of type. No. Let's see. Did I not save the policy? Let's check. Bookmark policy. Perhaps I didn't save it. There we go. So we will have this, this action is unauthorized. And now you have this clean, simple way of authorizing or unauthorizing specific actions.
So now if I was to refresh and try to click the delete button without modifying it, it works just like we would expect. Last thing, two more LiveWire directives, HTML directives that I really, really like. It's going to be the wire confirm and wire loading actions. So within our LiveWire class, we can call any PHP, any Laravel code over here. We've already done some of it. I mean, we have this auth login user. We have, our.
ORM, we have eloquent queries anything over here is server side PHP Laravel code. So some of these things can take a little bit of time to run. For example, let's just say, okay, we have all these bookmarks. Let's just ignore the word bookmarks. But what we want to do is create a, uh, we'll create another form.
Here, and we'll just go ahead and say wire submit equals a send notification and we have a button that says send Notification Now again, not very practical for us, but this just shows how this can be done and is helpful within Livewire. So we have a button that says send notification. Now, if we were to go and create a new send notification over here, what we want to do is create a notification within Laravel to test this out.
So let's go ahead and say php artisan make notification. We just want to say annoy user because this is just a button to send a notification to ourself, would you like to create a markdown view? No, and we're good to go. So let's go ahead and go to our annoy user notification. We just want in our mail message to say, hi there. Uh, You have a new bookmark and you have an action.
I hope this wasn't too, I hope this wasn't too annoying. There we go. So this is just going to send an email to a particular user that we're sending this to. Okay. The whole point of this, this isn't a crash course on notifications. The whole point of this is to show that our methods here, so we're saying auth user, we're going to notify them this annoy user method. Perfect.
Now. What we're doing here is essentially saying that we are running some background job. We're notifying the user and we can, of course, queue up these notifications. We could even use the fancy defer method that Laravel just has that's kind of new out of the box. But what if this took a little while, maybe we can just, uh, for fun, just sleep this for two seconds, if this takes a little. Longer.
We don't necessarily know what's happening on the front end. Let's go ahead and run this. So we have the set notification button, send notification 2 seconds. This is buttons kind of disabled while it's happening, which gives us a little bit of indication that something did happen. We should see an email in our mail trap testing folder. Let's go check that. So we do have an annoying user email. Hi there. You have a new bookmark. I hope this wasn't too annoying.
Isn't that great. We're able to test our emails in a browser ready to go for us. We don't have to think about it. Okay. But what happens when, how can we let the user know that something is happening in the background? For example, maybe our site's running a little slow. Maybe it took five seconds to do this. Yeah. The button is disabled, but can we do anything more? And. Yes, we can. First, why don't we just add something really neat on here.
We can say, wire confirm. And we're going to say, are you sure you want to send this notification? That's one thing I want to show you. That's a live wire attribute. That's really neat. But then we also have, uh, the ability to say, Maybe a P tag and we'll say this is being sent and we can add this wire loading attribute.
What's going to happen here is any time we have a method being run on the backend, we can show a particular element. In this case, this paragraph is saying this is being sent. Maybe it's, well, that's some periods just to show it's happening in real time. But when that method has been finished, this is going to be removed. We can even target our specific methods. We can say wire target equals send notification. Let's take a look.
Refresh send notification. This is being sent to test user two and taking five seconds. So we have this loading. So when this is loading, it is being sent automatically. And we have new email, annoy user. Now it looks like my wire confirm didn't work because it needs to be on an actual wire click event.
So why don't we take this whole button out and we'll go ahead and just send notification straight from a button. So wire confirm. Let's go back to our page. Notify. Are you sure you want to send this notification? It's a great little dialogue. It's a little helper function that LiveWire gives, and we can click yes, and we still see even outside of this button, it's in a different element.
It's in our form, and we still see that loading happen. We should see a third email and mail trap, and we do. So wire loading and wire confirm. Not all of the wire directives. There's so many more, but it's a great start.
There's so many more things within Livewire that we could dive into and spend hours learning each intricate part, but hopefully this gives you a brief overview, a crash course into the majority of things that you're going to reach for when building an application within Laravel and Livewire.
For example, some of the things that we didn't touch on that would be great if you wanted Or the ability to upload files or have some of the state that we talked about to be specifically linked to URL query parameters. There's so many more things like being able to have a specific wire attributes, directives, such as pulling the server so that you have information up to date.
Concepts like nesting components and how do components get information being passed from one component to the next, not just side by side components like we talked about within events. The neat thing within LiveWire is that everything that happens within the server, in this case, the class for a component is just pure PHP and pure Laravel.
So you can still Run methods from your front end to do things like send emails with mail trap, for example, or you could have the database be updated or fetched or deleted. And we talked about some of those things, but there's so much more you can do when you're running queues or jobs or anything else within your back end that you can essentially call, run and initiate from your front end, all without having to really touch.
That much code. Let me know in the comments, if there's anything specifically that I need to dive into in future videos, when it comes to learning pieces of live wire. And thank you again for mail trap for generously sponsoring this video, please check them out and thank you for watching subscribe.
If you can, and until next time I'll see you, but keep creating.