YouTubevideo· Jun 26, 2024· 33:58
I actually really like the idea of React Server actions and React Server components, specifically how Next.js is starting to implement them, because it starts to move the web and the JS ecosystem forward as a whole to using the server more. And I really love Laravel and Livewire, and specifically Livewire Vault, and how you have a single page and then individual functions or methods that you know are being run on the server.
Now, the director of DevRel from Next.js, Lee Rob, if you don't know who that is, make sure you subscribe on YouTube and follow him on Twitter. Puts out fantastic content, great individual. He made a video talking about the blazing fast emoji to-do list. Basically, just a quick little introduction on here's what React server components are, specifically within Next.js. And then here are server actions. basically functions that you can call within your client side component but you know that that code
gets run on the server and thus I love the the architectural structure of this because it fits so well with Laravel and Livewire I want to do a comparison what if we built this but in Laravel and Livewire. Okay, the application premise is pretty simple. You have a single input field here where you can input to-dos. Of course, you can automatically input it with your keyboard,
but it's client-side validation as well as server-side validation to say that you can't actually input anything but to-dos. I'll just input one right here. You can see that it automatically shows up there at the top. And all of this is happening on the server, pushing it to a database and then pulling from a database when it's redirecting to this page.
Now, one of the key things that we'll just say right off the bat is server actions and server components are meant to be used in a way that if I was to turn off JavaScript, everything just works. Now, Laravel and LiveWare specifically doesn't work in that way. Of course, you can make that work. And Laravel Blade forms, for example, are just that. They don't use JavaScript if you don't want them to, but Livewire does need JavaScript. So that's going to be one of the biggest differences right off the gate.
I want to get out before we let that happen. So why don't we take a look at what this code actually looks like, and then we'll implement it in Laravel and Livewire. So let me pull up my terminal. And first off, let's just start off a new Laravel folder, Laravel application, so we can have the premise and the structure for building out this application. So we'll say Laravel, let me actually go into my code folder. We'll say Laravel new emoji to do Livewire. Perfect.
We'll start off with Laravel Breeze because this gives us authentication. And maybe we want to, as kind of like a bonus feature, add authentication so we know who created those emojis in this to-do list. So we'll go Livewire Volt Class API. We'll have no dark mode, but PEST and we'll initialize a Git repository. While that's installing, let's go ahead and pull in the emoji to do to do. This is from Roush G, who is the CEO of Bursell.
So why don't we pull that up? And I'm just going to pull that up in my VS code here. Let's take a look. The premise of a Next.js application is it has this layout file. And you'll see some red squiggly lines because I haven't actually installed this application. So bear with me. But what we're doing here is this layout file that basically just wraps all of our page.
And this would be similar to like a index.blade.php file, for example. You're not necessarily putting a LiveWire component as a single page. You can and you might, but we'll probably have a LiveWire component nested in that page. Now we have that page.tsx, and this is where that holds most of this application and the structure. That's where we're saying on page load, grab the to-dos from the database. You can see that this is an async function, which is basically just saying it's awaiting
this particular function to be run. And then it's passing those to-dos into the page. It has a form with an action of submit. You can see here that this action goes to this actions.ts file over here, where it's pulling from the database or I should say grabbing the text from the HTML form and then if it's actually an emoji, if it passes this regex expression, then we'll insert it into the
database. Okay, let's implement this in Livewire. So now that we have our database set up for our Livewire application, why don't we pull that up in another code on the left and we'll kind of just start moving things over and just saying, okay, what's the differences? What's the similarities? Obviously, the code syntax is going to be the biggest one, but let's think from an architectural
standpoint. How does writing this code make you think or how do you start to write this code as a developer? And I think the DX, the developer experience between these two structures, these two way of writing is very similar. Obviously, Laravel provides a lot more on the backend when it comes through things like email and queues and everything like that out of the box where Next.js does it. But the server components itself feel very similar to Livewire and LivewireVault in my
opinion. Let's see if you feel the same way. So emoji to do Livewire. We'll run npm run dev just so it's all running for us. And then we'll open this up in our code editor over here. Okay. Now we have our routes web.php. We're just going to use this welcome route as our let's say that that
would be like our layout or our page file. So let's go ahead and pull that up. So we have the welcome. I'm just going to gut this whole thing. So everything within the body, I'm just going to gut. I'm going to remove this class and I'm just going to say hi there. So let's see what this looks like. So first off, in our, I'm going to say herd open, that's for Laravel herd,
this is going to open it in our web browser. Okay, so let's pull this up here. Perfect. So we have either this is running as expected. So basically, we want to make it look really similar to this emoji to do. So why don't we start pulling that in? Here we go. So first off,
let's look at the page.tsx file here. And it has all this main function. So why don't we go ahead and create our LiveWire component and use that instead of writing everything in this welcome.blade.php. So in our terminal for LiveWire, we can say php artisan make bolt. And so that's the volt component a single file component that we'll be putting all of our logic in. I'm going to call this,
why don't we call it emoji to do. Very simple. Okay, so now instead of hi there, the way that we include a LiveWireVault component is going to be LiveWire. And we'll call this emoji dash to do. Perfect. So now anything that is in our emoji to do blade.php file, this is our LiveWire file that generated for us, we can start to build this out. And what you'll see is that we will very
rarely leave this file. I think it's really similar, at least in, in structure and feel, when it comes to how you're building things as this on the right, where you have all of your code. And of course this is hitting the server because you're grabbing all the to do's from the the to-do table, you're connecting to the database in this function. Same thing with a server action.
When you're writing this to the database, it's on the server. All of this also happens on Livewire in the same way. So I think it feels really similar, just in how it looks as well. Of course, I opted for the class style API, but LivewireVault has a functional style API that might feel a little bit more similar. You're not writing everything in a class. but if you use Svelte 5, the class structure feels a lot similar as well.
So let's pull up that page.tsx again. Let's just grab all this to make it look really similar. We'll put this in main and let's take out what's not React. So this, or take out what is React, I should say. So emoji to do, we'll say Livewire and we'll put this to the GitHub that I'll add it to Josh Siri emoji to do live wire. Perfect. We'll remove the to do's map. And
what we'll do here is actually a for each. And we don't have that yet. So we'll eliminate that. But we'll remove the key. And then this is the to do text here. Perfect. Then we don't have an action in live wire, but we'll add that later. The difference here will be this will be 10. So a quotation instead of the curly braces, all of that is just standard HTML. So input type of placeholder.
And let me actually, what's it called? Wrap. There we go. It's a lot easier to read now. Perfect. I'll do the same thing for this. Puggle word wrap. Perfect. Okay. Now, I believe they're using some styles on this one right here in the global.css.
So why don't we take all this and we will apply it to our app.css over here. This is where all of our tailwind styles are, but that's just to make it a little bit clearer for the demo, I believe, in the Next.js version. So we have emoji to-do list. Perfect. We have this form. We have all of that. Awesome. Let's take a look at what this looks like in the browser. Okay. It's looking very similar.
I don't know why this is not bolded. It probably is because, hmm, like a main H1. It might be just because how we're implementing it in the body here. It's not applying down. We'll ignore that for now. Okay. So let's take a look at the emoji to do, and let's start to add emojis. Well, first off, we need the actual database. So this is using drizzle.
And what that is doing here is in the db.ts is where they're defining that to do's table. So let's see how we would do this in Laravel. We're not actually creating any singular file to do all this. We can do it with the Laravel CLI. We can say PHP artisan make models. That's going to be similar to this to do's table function right here is it's not exactly a one for one comparison to what a model
is, but it's halfway there. And in the model, we're going to call this a to do so it's going to be the singular version of what we want the table to actually be called dash M for migration, we'll create that. Okay, so to do model. Now this is basically just saying this is what is allowed to happen on the model or maybe any kind of helper functions or methods that we want to add. But first, we'll go ahead to the to-dos table. This is where we can start to add what is shown here, where it has an ID and text.
Now, Laravel has already generated an ID for us and some timestamps. We'll just leave those just because it's something Laravel gives, but we need a table with a string of text and we'll say this is set to 10 characters. Perfect. Now, how do we run this migration? In Drizzle, you're probably using something like, you know, npm drizzle db-push or dash migrate or something like that. But we do that within php artisan migrate. Perfect.
Now we actually have a to-do table in our database. And now the last thing we need to do, and this is something that I don't think is very common practice within a lot of like JavaScript ORMs like Drizzle or Prisma. the ability to say, okay, I've created this in my database, but now I actually only want specific
fields to be writable or to be fillable. And here we can have the ability to say a protected fillable is text. So this is just saying the only thing I actually want to write within any of my code, whether that's user generated or just on the server, is only to be text to fill into this to do table, to do's table. Now, of course, there's other ways of saying this. You can also say guarded, which is basically the exact opposite to say, okay,
anything can be written to the to do's except for what's ever in this array. I usually use this just because most of the time, any table or field that I am creating within the specific, any column, I should say, within a specific to-dos table or specific table that I'm creating. Usually I'm writing to that as part of my application. Okay. Looks like we are good to
go. So why don't we start actually implementing this within our create to-dos or within our emoji to do list. Perfect. So here, similar to how this page dot TSX would show this await DB and grabbing to dues limit of 20 order by it looks like it uses search params to say if ascending is
true. So ASC equals true in the search params, then it's going to set it to ascending. Otherwise, it's going to set it to descending. So we'll just say descending is true. We won't do the params just yet, but we'll say public function with, this is basically just saying anything, anytime this component re-renders within Livewire, whether that is another function or a method causes it
to re-render or when it's just loaded, that's going to run this function. We're going to say it's going to return with to do's and those to do's are the to do model all. Now here we can say that we want to use that model. So app models to do. And so this is a great way of, it's very similar to maybe what this DB from to do's table would be, but instead of calling that
actual query ourself, we're saying we want the to do model. And that's just saying instead of writing out the actual query saying select from the to do's table and then get all that stuff. I just want to grab all the to do's based off of that model that we create. So just a simpler, cleaner way of writing this. So in order to, instead of to do all in order to get the limit
of 20, we'll say to do with a limit of 20 and we'll say order by ID of descending and then we'll get those. Okay, now we don't actually have any to do's in our to do table just yet. So why don't we hook up the actual server action method equivalent within Livewire. So we have this, which is saying I'm grabbing all the to do's now we'll actually hook up this server action. Now,
this is going to be a little bit different because the form action, like I said prior, this uses HTML behind the scenes. So if JavaScript is disabled, it just uses strict HTML. That's not going to be the case within Livewire, but we'll kind of see what the differences are. So first, we need to hook up this input to a particular field or variable within this Livewire component.
So we can say public string of text is equal to, and we'll just start it off, initialize it as blank. Now here's where we can actually add that validate function. So this is going to be similar to this server function where we validate the regex here. But for now, we'll just say
that this is required. We need to use Livewire attributes, validate. Perfect. So what do we want to have for this text, we want to wire it, we want to sync it. And this is going to be called wire model. And we're going to call text. This is just saying this input should link
to this text. And what that's going to do is whenever we run a method, in this case, hit this button or submit this form on that button press or that form submission, we want to sync whatever's in this input to the text string so that we can do whatever we want with it on the server. Again, anything in this class component is going to be run on the server. So now we can actually call the submit method. So similar to that, we can call public function
submit. And we're just going to say first that we're going to validate whatever is being submitted with this form. In this case, only text is being submitted. And then we're going to do create. And here we can run something called this hole. Now that's a fancy little LiveWire helper function that basically says I'm grabbing whatever variables, whatever strings and input that we're pulling in on this form.
I'm grabbing it and then resetting that input. So it's pulling it from the field so that there's nothing else in it. So, and that's what this is doing right here. It's redirecting, which is essentially clearing that input. I believe there's other ways to do this with the next JS where you can revalidate the page, not necessarily have to redirect, but that's what this implementation is doing right here.
So we have this input wire modeled, but now we can wire submit equals submit. And what this is going to do is anytime this form is submitted, and here's one thing I actually didn't know until right now, if you have a singular button within an HTML form, it automatically chooses that as a submit button. You don't actually have to have the type equals submit, which I thought you did. So that's
something I learned. So anytime this button is pressed, wire submit is called, which runs that submit method, which you see up here, or anytime the form, an enter button is pressed within the form. So let's take a look. Let's see. Let's hit emoji here, call this. Okay, nothing actually
happened because you can see here where they're not actually pulling those emojis out. We linked them to the page with this with function, but we're not actually showing them. So here's what we're going to do. We're going to say for each to dos as to do. And then here we'll call to do text. And instead of actually to do text, we want to make sure that this is a wire key of to do ID
and then to do text. I'll show you what this, the reason for this. This is very similar to the page.tx key. This is just saying we need to link it to a key so that there's a representation of how many to do's are on this page. So that way we have the given ID relative to this
loop of to do's. So to do's map is very similar to how for each works in a blade component. This is Laravel specific and not necessarily live wire specific for each to do's as to do. We're just setting the key to the to do ID. And then we're just setting the to do text. Very similar, just different syntax. Okay, there we go. We have that emoji to do that we input.
And let's see some eyes. And okay, that doesn't work. That does not work. Again, there are some, you can see here, there's numbers that can be submitted within that validation just because this regex isn't perfect. Emojis have numbers, so they're like part numbers. And so this regex is good enough, I should say. It's not perfect.
You can still input numbers and other characters, I believe. But we're going to grab that regex and use it for our validation. And this is something that's really cool within Livewire is basically it's saying we have this attribute within PHP, anything that's kind of like this hashtag and then in here is an attribute above. This is saying this next thing, in this case, this public string text should be validated like this.
So we can actually call required and we can say regex and then we can call this emoji regex similar to how this is being called here okay so we have this emoji regex we're saying setting setting the text to string and let's see we can call the thinking emoji
perfect okay and it's ordering by descending there we there we go so looks great i'm liking it Just to polish things off, why don't we go into our welcome.blade.php, and instead the title, we'll just call this Emoji To Do Livewire. There we go. Okay, but what if we wanted to add that query string parameter?
Even though it's not talked about much in that video, there's this search parameter where it's saying, If ascending ASC is equal to true in the query parameters of the URL, then we'll set the order to ascendings when we do that. So first we're going to pull in this val, this, excuse me, this second attribute here that is URL. So liveware attributes URL, then we can say URL
and we'll go ahead and say this. And we'll just set that to URL and then public. We'll say this is ascending. This is just saying that if ascending is automatically going to be set to null. Perfect. This is just saying that there's a URL string parameter that can be ASC. And actually we'll just set this to false.
Perfect. So now we can say if public ascending is equal to true in our public function with public function with, then we will order this by ascending instead of descending. And so what we can do here is that of just returning this, we can say that the sort direction should be this ascending and if it's ascending or otherwise it'll be descending.
So we're going to order by the ID based off of what the sort of direction is set. So we're going to go back here and we would say ASC equals true. All of a sudden, it's reverted. It's inverted because the ASC parameter in the URL is set to true. Otherwise, so this has the same functionality. ASC equals true. There we go.
Again, just a nifty little feature that is very similar to this search params ability here where it's saying the ASC should be a string. And here we just have actually set to a Boolean. That way we have this true or false indicator within the URL. Again, this attribute is just saying that that is set in the URL. And then we're just checking for it here.
This ascending is referencing this particular public variable. Okay, now that everything is similarly true in both instances, why don't we take this one step further? Maybe we'll add some authentication and say, well, we can only actually submit an emoji if we're logged in. Well, why don't we do that? Let's go ahead and clear this. That way we can just have this in full page or move this and we'll just full page this
right here. Perfect. Now we already have our authentication set up. So the first thing we're going to do is we're only going to see this form if we're logged in. Otherwise, we shouldn't be able to see this form. So we can actually have this at auth. Else, then maybe we'll just say here, we'll see what Copilot gives us at and off. So this is basically just saying, if you want to
To see this form, you should be authenticated. This is a little helper function that Laravel Blade gives us. Let's take a look. Okay, we're not actually logged in, so I'm going to have to log in to add a to-do. So why don't we actually make this look a little bit cleaner and maybe add some 20. There we go. Log in to add a to-do. Perfect.
it. And okay, this looks a little, this doesn't look that great. I believe it's probably because of the I believe it's probably because of the app dot CSS applies that we're doing here. So when we pull in a package that I actually just created a video about, you can find that up there. But this is a authentication package that makes just our off pages look a little
bit cleaner. So we can use dev dojo auth. We're going to use the Laravel breeze install, set this up. So we'll go to our command line. All right, those require auth, we're going to need to run these might publish these settings, and then we're going to migrate and just set this to the user. So run those publishing,
going to PHP artisan migrate. Perfect. And then in our code, we just need to on the user model, we're going to extend auth user and that's going to be the leave that the let's see what that is. That's the dev dojo auth models user. Okay, perfect.
So I'll set that here. Great. That should work. And now if I was to go back to our page, refresh. Actually, it looks like, oh yes, we need to in our auth.php route, we need to remove all of the guest middleware. That way it doesn't overwrite
what this package is trying to accomplish. Okay, refresh, login, add it to do. And we have this fancy little page so we can sign up. And why don't we actually first go to slash slash setup because we need to set the name field during registration. And this is something that Laravel Breeze sets up out of the box. So to be compatible with everything that's been set up, we need that name field.
And why don't we bring that into our emoji to do's when we list them out. Okay, so main page, log into that to do. I wanna sign up. My name is Josh C. Perfect. And we're automatically redirected to the main page. Now we have this emoji to do form visible. Perfect, now I can actually add this emoji.
Great. Now, how can we link these emojis with our authenticated user? Now that we have authentication already set up, let's take a look. So in our to do blade, we'll just go ahead and say we want that shown here. So maybe we'll wrap this in a, oops, we'll wrap this in a p tag. Perfect.
And we'll have another tag here of maybe to do user name. Perfect. and then we'll have a div here with a class of flex item and setter and we'll say justify between perfect now we don't actually have these to do's linked to a user you can see here attempt to read
property name on null because there is no user associated with these to do's so let's fix that In our to-do table, our migrations, we're going to add a new column. That's foreign ID. And it's going to be linked to the user ID. So what we can do now in Laravel with the PHP Artisan CLI, we can say PHP Artisan migrate rollback.
That's going to roll back the to-dos or the migrations that we already did. And that rolled back the ones with the authentication passage. So what we're going to do is actually migrate fresh. This is just saying we're starting from scratch. All migrations are being run again. So we won't have our user set up. Migrate fresh. Perfect. But now all of our to-dos are associated with a given user.
Now if we were to go into our to-do model, we can now say we want this associated with a user. And this is just saying, this is a little helper function to say all to-dos now can call this user method with it. So if I was to call to-do user like so, to-do user, I would automatically get everything about that associated user. It's a little helper function to call database queries
without actually having to call the database query. We can do the same thing on our user model. So we can say public function to dues returns has many so a user can have many to dues now. Okay, in our emoji to do list. Now we can say instead of just grabbing this, now we want to grab with users.
Okay, there we go. Perfect. So to grab the to-dos with the users table, limit those by 20 and then order by the sort direction. Let's take a look. Well, there's no users or there's no emojis actually created. Let's go ahead and log in and add one. Sign up. See, create this password real quick. and now we can start adding to do's.
And looks like, okay, yes, we're not actually adding to do's with a user attached to them. So we're pulling the to do's with a user attached, but we're not actually adding them. So what can we do to create this to do now and actually add it to the user? Well, we could do something like, we could do something like this where we set the user ID manually, but there's a helper function
that makes this a little bit easier. We can say auth user to dos create this stuff. And what this is saying is that I want to create a to do that's now associated with this authenticated user. So now a user ID can't just be passed in manually within this given method from the front end. It actually has to be the authenticated user based off of these Laravel conventions. So now
Let's take a look back at our emoji. We'll add this. Undefined relationship users on model. At model studio. Let's take a look. Maybe we didn't save it. To do with users. Oh, that is why. So what we really want here is not to do with user. We just want the actual user of it to do. And there we go. Josh C. So hopefully this gives you a good overview
of how things like Laravel Livewire, specifically with Volt, is very similar from a developer experience perspective as React server components or React server actions. You have this clear defined line while it's still very much co-located of what is server and what is not. Who knows? We might be able to keep building on top of this project with things like rate limiting or deploying or things of that nature. But until then, keep creating.
Amen.