YouTubevideo· Jun 26, 2024· 33:58· 4,300 views on YouTube

Converting a NextJS App to Laravel Livewire

Watch on YouTube ↗
0:00

React at a moment you like. The best moments light up here.

transcript

82 moments
  1. I actually really like the idea of react server actions and react server components specifically how nextjs 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 larl and Livewire and specifically Livewire volt 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 devil from

  2. next JS leer Rob if you don't know who that is make sure you subscribe on YouTube and follow me on Twitter what's that 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 nextjs 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 since I love the the architectural structure of

  3. this because it fits so well with larl and Livewire I want to do a comparison what if we built this but in larel and Livewire okay the application premise is pretty simple you have a single input field here where you can

  4. 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 you can't actually input anything but to-dos I just to input one right here you can see that it automatically shows up there at the top

  5. and all 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 sh 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 larel and live R specifically doesn't work in that way of course you can make that work and larel blade forms for example are just

  6. 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 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 Lille and Livewire so let me pull up my terminal and first off

  7. let's just start off a new larvo uh folder Lille application so we can have the premise and the structure for building out this application so we'll say Lille um let me actually go into my code folder we'll say larel new Emoji to do live wire perfect we'll start off

  8. larel Breeze because this gives us authentication and maybe we want to um 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 live wi volt class API we'll have have no dark mode but Pest and we'll initialize a get repository while that's installing let's go ahead and um pull in the Emoji to-do to-do list this is from Rous G who is the CEO of verel so why

  9. 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 an exj application is it has this layout file and you'll see some red squiggly lines because I haven't actually um installed this application so bear with me uh but we what we're doing here is this layout file that basically it just wraps all of our our page and this is be similar to like a index. blade.

  10. 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 uh application and the structure of this where we're saying um on page load grab

  11. the to-dos from the database you can see this is an async function which is basically just saying it's awaiting this particular function to be 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.

  12. TS file over here where it's pulling from the database or or or I should say grabbing the text from the HTML form and then if it's actually an emoji if it passes this Rex um expression then we'll insert it into the database okay let's implement this in Live Wire so now that we have

  13. our um 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

  14. 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 larvo provides a lot more on the back end when it comes to things like email and cues and everything like that out of the box where nextjs does it but the server components itself feel very similar to live wire and Livewire volt in my opinion let's see if you feel the same way so Emoji too

  15. Livewire we'll run mpm 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 um as our let's say that that would be like our layout or our page file um so let's go ahead and pull that up so we have the welcome I'm just going

  16. 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 lville herd this is going to open it in our web browser Okay so let's pull this up here perfect so we have hi there this is

  17. running as expected um so basically we want to make it look really similar to uh to this Emoji Todo 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.

  18. PHP so in our terminal for the Live Wire we can say PHP artisan make volt 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 um why don't we call it Emoji too very simple okay so now instead of hi there the way that we include a live wire volt component is going to be Live Wire um and we'll call this Emoji Das too

  19. perfect so now anything that is in our Emoji too 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

  20. 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 the the to-dos from the to-do table you're grab you're you're Set uh connecting to the database in this function same thing with a server action when you're writing this 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 Livewire volt has a

  21. functional style API that might feel a little bit more similar you're not writing everything in a class um but if you're used uh if you've used spelt 5 the class structure feels a lot similar as well so let's pull up that page. TSX

  22. 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 uh or take out what is react I should say so Emoji Todo we'll say Live Wire and we'll put this to the uh GitHub that I'll add it to Josh Siri Emoji to-do Live Wire perfect we'll remove the

  23. to-do's map and what we'll do here is actually a four each and we don't have that yet so we'll we'll eliminate that uh 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 um what's it called wrap there

  24. we go it's a lot easier to read now perfect I'll do the same thing for this um toggle word wrap perfect okay now I believe they're using some styles on uh this one right here in the global.

  25. CSS so why don't we take all this and we will apply it to our app.css over here so this is where all of our tail one styles are but that's just to make it a little bit cleaner for the demo I believe in the nextjs version so we have Emoji Todo 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

  26. similar uh I don't know why this is not bolded it probably is because because H like a main H1 it might be just because how we're implementing it in the in the body here it's not 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.

  27. TS is where they're defining that to do table let's see how we would do this in larvo we're not actually creating um any singular file to do all this we can do it with the larl uh CLI we can say PHP

  28. artisan make models that's going to be similar to this to-do table function right here 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 be called- M for migration we'll create that okay so to

  29. 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 and this is where we can start to add what is shown here where it has an ID and text now larl has already generated an ID for us and some time stamps we'll just leave those just because it's something laral gives but we need a table with a string of text

  30. and we'll say this this is set to 10 characters perfect now how do we run this migration in drizzle you're probably using something like you know mpm drizzle db- push or- 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 um

  31. a lot of like JavaScript ORS like drizzle or Prisma the 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 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 you

  32. generated or just on the server is only to be text to filled 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 todu except for whatsoever in this array I usually use this just because most of the time any table or field that I am creating within a specific uh any column I should say

  33. within a specific Tod 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 todos um or within our Emoji Todo list perfect so here similar to how this page.

  34. TSX would show this await DB and grabbing Tod Do's limit of 20 order by it looks like it uses search prams to say if a sending is true so ASC equals true in the search

  35. prams 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 prams just yet but we'll say public function with this is basically just saying anything anytime this component renders within Livewire whether that is another function or a method causes it to render or when it's just loaded it's going to run this function we're going to say it's going to return with todu and those to-dos are the to-do model all now here

  36. 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 uh query ourself we're saying we

  37. want the to-do model and that's just saying uh instead of writing out the actual query saying select from the to-dos table and then get all that stuff I just want to grab all the to-dos based off of that model that we created 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 too with a limit of 20 and we'll say order by ID of descending and then we'll

  38. get those okay now we don't actually have any to-dos 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-dos now we'll actually hook up the 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

  39. scen so if JavaScript is disabled it just uses strict HTML that's not going to be the case with Live Wire but we'll kind of see what the differences are so first we need to hook up this input to a particular field or or variable within this Live Wire component so we can say public string of text is equal to and

  40. 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 Rex here but for now we'll just say that this is required and we need to use Livewire attributes validate perfect so what do we want to

  41. have for this text we want to wire it we want to sync it this is going to be called wire model and we're going to call text so this is just saying this input should link to this text and what that's going to do is whenever we um run a method in this case hit this button or submit this form on that button press or

  42. 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 to-do

  43. create and here we can run something called this hole now that's a fancy Little Live Wire helper function that basically says I'm grabbing whatever Vari 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

  44. 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 nextjs where you can revalidate the page and not necessarily have to redirect but that's what this uh implementation is doing right here so we have this input wire modeled but now we can wire submit equals submit what this

  45. 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 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

  46. is pressed within the form so let's take a look um let's see let's hit an emoji here call this okay nothing actually happened because you can see here where they we're not actually pulling those emojis out we 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 do as to

  47. 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.

  48. TX key this is just saying we we need to link it to a key so that there's a representation of how many to-dos are on this page so that way we have the given ID relative to this uh

  49. loop of to-dos so to-do's map is very similar to how for each Works an a blade component this is Layel specific and not necessarily Live Wire specific for each to-dos 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 inputed and and let's see some eyes and okay that doesn't work that

  50. does not work um again there are some you can see here there's numbers that can be submitted within that validation just because there's this rejects isn't perfect it's and emojis have numbers so

  51. they're like part numbers and so this this Rex is good enough I should say uh it's not it's not perfect you can still input numbers and and other characters I believe but we're going to grab that Rex and use it for our validation now this is something that's really cool within Livewire um is basically it's saying we

  52. 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

  53. Rex and then we can call this Emoji Rex similar to how this is being called here okay so we have this Emoji Rex we're saying setting setting the text to string and let's see we can call the thinking Emoji barard okay and it's ordering by descending there we there we go so looks

  54. great I'm liking it just to polish things off why don't we go into our welcome. blade. PHP and instead of the title we'll just call this uh Emoji too Livewire there we go okay but what if we want to add that query string parameter

  55. um even though it's not talked about much in that video there's the search params where it's saying if a sending ASC is equal to true in the query parameters of the URL then we'll set the order to ascending why don't we do that so first we're going to pull in this Val this um excuse me this second attribute

  56. here that is URL so liveware attributes URL then we can say URL and we'll go ahead and say this H we'll just set that to URL and then public uh we'll say this this is ascending so

  57. this is just saying um that if ascending is automatically going to be set to null perfect so 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

  58. 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 instead of just returning this we can say that the sord 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

  59. what the sord direction is set so if we were to go back here and we would say um ASC equals true all 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

  60. little feature that is very similar to this search prams ability here where it's saying the ASC should be a string and here we just actually set it 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 again this ascending is referencing this particular public variable okay now

  61. 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 we do that let's go ahead and clear this that way we can just have this in full page we'll remove this and we'll just full page this right here

  62. 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 o else then maybe we'll just say here we'll see what copal gives us at and off so this is basically just saying if you want to have see this form

  63. you should be authenticated this is a little helper function that Lille blade gives us so let's take a look okay we're not actually logged in so I'm going to have to log in data 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

  64. do perfect and okay this looks a little this doesn't look that great I I believe it's probably because of the um I believe it's probably because of the app.css applies that we're doing here so why don't 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 o

  65. we're going to use Lille Breeze install set this up so we'll go to our command line all right those require off we're going to need to run these mic or publish these settings and then we're going to migrate and just set this to the user so run those publishing going to PHP

  66. Artisan migrate or and then in our code we just need to on the user model we're going to extend off user and that's going to be the believe that's um the let's see what that is that's the dev Dojo ath models user

  67. okay perfect so set that here great that should work and now if us to go back to our page refresh actually looks like oh yes we need to in our auth.php route need to remove all of

  68. the guest middleware that way it doesn't overwrite what this package is trying to accomplish okay refresh login added too and we have this fancy little page so we can sign up and why don't we actually first um go to SL SL setup because we

  69. need to set the name field during registration now this is something that Lille 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 in to our Emoji todos when we list them out okay so main page loging in at Todo I want to sign up my name is Josh

  70. 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 um we'll wrap this in a oops we wrap this in a P

  71. tag perfect and we'll have another P tag here of maybe to do user name perfect and then we'll have a div here with a class of flex item Setter and we'll say justify

  72. between perfect now we don't actually have these to-dos linked to a user you can see here attempt to read property name on null because there is no user associated with these to-dos so let's fix that in our to-do table our migrations we're going to add a new column that's foreign

  73. ID and it's going to be linked to the user ID so what we can do now in uh Lille do with the PHP Artisan CLI we can say PHP Artisan migrate roll back that's going to roll back the to-dos or the migrations that we already did um and

  74. that right rolled back the ones with the authentication passage so what we're going to do is actually M migrate fresh this is just say we're starting from scratch all all migrations are being run again so we won't have our user set up migrate brush perfect but now all of our

  75. to-dos are associated with the given user now if we were to go into our to-do model we can now say we want this associated with the 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

  76. 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 todos returns has many so a user can have many to-dos now okay in our Emoji

  77. to-do list now we can say instead of just grabbing this now we want to grab with

  78. 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 so let's go ahead and log in and add one sign up C create this password ra quick and now

  79. we can start adding to-dos and looks like okay yes we're not actually adding to-dos with a user attached to them so we're we're pulling the to-dos 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 little bit easier we can say off

  80. user to do create this stop pool 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 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 Layel conventions so now let's take a look back at our our Emoji We'll add

  81. this undefined relationship users on model app models to do let's take a look maybe we didn't save it um 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

  82. like larvo 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 collocated 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 \[Music\]

just you here