YouTubevideo· May 16, 2024· 17:58
let's build an authenticated to-do list in LL Live Wire and Live Wire volt using Breeze from the get-go so what we're going to do is start with nothing and create a new laravel project and then see how fast we can actually make authenticated to-do lists for each individual user and we'll learn some things along the way but hopefully this gives you an idea of how Lille uses things how you get out of the box with
larl and then what you might expect from larl compared to like a scpt framework that you're used to using so let's go let's start with Lille new to do list we just go for that we're going to use larel Breeze we're going to use the Livewire volt class API um you can use the functional API if you want but I'm a little bit quicker in the class API and we'll do no dark sport
pest we'll initialize GitHub repo and or get repository we'll go and what we're going to do is we're going to start off by creating a new to-do model we're going to use SQL light and we're going to say okay within this model I actually want to save these to-dos to the database but they have to be associated with the user so what does that look like and we'll see what get uh what larel Breeze provides out of the box but then we'll also see what are the things
the steps you need to take to start making something within larel and what does that look like compared to like a traditional JavaScript framework for example so let's go ahead and go into our to-do list we'll go ahead and PHP Artisan migrate which migrates all of the database things that you're looking for has already been run that Lille Breeze provides out of the box that's things like users and a few other things that we're not touching but what we're going
to do is create a model what that means is we're going to give our project our folder something to associate with it to do and that's a model and then we're going to do dasm which is basically same create a migration for that as well so if I was to go head and open our code what we're going to look at is the to-do migration which is the create to-dos table Lille provides these migrations out of the box and any new
migration we create just gives you this to start with and what we can do is add new uh table columns here so we're going to go table string and we'll actually go ahead and call this name now we want to associate this with a user so what we're going to do is table foreign ID which basically just says hey I want to associate with this another another model I want to say that's the user ID which automatically Associates it with
the user model and we're going to have it Cascade on delete and constrain which basically just says anytime the user is deleted we're going to also delete that users to-dos okay before we get moving forward in to-do model what we have to say is okay this to-do model in our database can actually be filled we can actually create to-dos one of the ways you can do that is with predective fillable I like to do protected guarded
which basically says um you can fill anything in this to do model except for the ID we don't want people changing IDs now in our user model what we're going to say is the user needs to have some to-dos associate with them so we can say uh public function to-dos and copy it's helping me out and this just says that okay when I call users Tod do I expect
that there's going to be many of them a user can have many to-dos we're going to do the inverse on the to-do model so public function uh user and we're going to say that this belongs to a user so that we can we can have the users todos and we can say that hey give me a to-do that belongs to this particular user we probably won't use this public function but we're just going to add it anyways now let's go ahead and actually start creating the
application so if I was to go back into my terminal we can actually say heard open and that's going to open um a new browser window where the application is running um let me go ahead and pull that up here so we can see it all okay now let's go ahead and create a new user we're just going to say Josh Siri Josh test.com testing password testing password and we should be good to go
we're all logged in we have authentication out of the box and now we have something to associate todos with uh if I was to go into my terminal I also want to run mpm run Dev which is saying anytime we're changing Styles because we have tail win I want it to live reload let's go into our code now we also want to add a volt component this is basically saying I want to build my to-do list and not have to worry about having multiple components where
some is having the actual server code and some's having the kind of the the view or the blade file this is would be similar to like a react server component so I'm just going to say PHP artisan make volt this is a CLI to kind of just generate all these files for us I'm going to say to- do manager and I'm going to pass in a D- class parameter now when that is created we have this
to-do manager blade file what this means is anything we write down here is markup and anything we write up here is server code that we can kind of talk to each other without having to do anything else so if I was to pass this into our dashboard view this is what we are seeing here you're logged in you're logged in so if I was to say
live wire and then to do manager now that Livewire component is here instead so if I was to go back to our code we go to do manager I'll say hi there then we have it all ready to to go okay let's start so first thing that I like to do is use this public function with command what this means is that any
time this function is loaded this component is loaded we actually pass in information to this component in this case we want all the to-dos so anytime we load this page anytime we load this component we want the users to do so what we're going to do is this returns array what we're going to want is a return um let's just say we want to return to dues and G is saying todu all that's incorrect because we actually just want
the to-do for that user so we can say off user and this is a helper function that laravel gives todos and we and we can just get those to-dos what this is doing is basically saying okay the logged in user Lille gives you this out of the box because you're kind of piecing this all together I want there to do is that's the function that we created in the user model and just get them for me basically give me all to-dos
so now we can do something like this for each and then we can do um to-dos as too and we can kind of just list those to-dos out here so we can say uh too let's should actually do a div so we can have um we'll just call this a another div
of to do name which is what we called that um and need double brackets here anything in larvo Blade is double brackets call that div and then we'll do a button here where this button is going to be deleting the to-do so wire click equals delete to do ID we'll
just say what co-pilot does here and then in this div we need to pass in The Wire key which is basically saying kind of similar to like a react key anytime we Loop over something we need key so we know which one is kind of associated with which one let's go ahead and take a look in the browser unexpected we have a syntax there I need a semicolon there let's take a look I know what we're doing we don't
need this semicolon there though there we go okay no such table to do is that is because we haven't actually migrated that new file that we did so let's go ahead and in our terminal we'll open up another one we'll just say PHP Artisan migrate perfect we have created our to-do table so let's go ahead and take a look now we have nothing because we don't have any to-dos associated with this user so why don't we go ahead and
create a way to do to do and L larel Breeze gives us components out of the box so we don't have to kind of create these um these kind of like inputs or anything like that so we have the ability to do uh in here we can look at components and we have things like text input and primary button so why don't we
do that we can do x- text-input um and we're going to model this with the let's just say that this is the to-do name and we'll go ahead and um close that off I'm going to create a form actually to do this so form wire submit this is just saying anytime the form is submitted using the button like submit button or just
pressing enter we're going to say create to do we'll create this function in a little bit um and then we'll go ahead and add a x- primary dasb button and we'll say create that's fine we want this to be type of submit perfect and now if I was to let's go ahead and actually add our to-dos because we're going to create a new
to-do what we can do here is kind of link it to a specific model so we can say use appmodel SL too is it say I'm linking it to the model that I've created in my Lille application and now I want to also create a string and this is kind of setting the state saying that I want a string of uh Todo name we can set it to blank for now okay let's take a look in the browser everything looks good um we're not going to see anything
because we do not have a we do not have a create Tod do so if I was to do this we probably would get an error yeah create does to do does not exist on component so what we can do is create that function public function create to-do now what we can do here is validate the to-do string so we can say um to do name is required and we can
actually say this also has a minimum of three strings so if I was to do this now uh if I was to just have one nothing's going to Happ happen because we don't actually see that error so how can we see that error well because we have this component out of the box there's a couple other ways to do it but we'll just do the component and what we can do is
x- um I think it's called input D error and we can pass in this message equals errors uh get to do name and let's see what that does let's see it does not do it so let's go ahead and see what uhuh there we go there we
go see what that does undefine variable messages okay undefine variable messages well let's go ahead and take a look at where the input errors used elsewhere and see okay messages messages equals errors G okay so that is the issue so we'll go back into our to-do manager oops
to-do manager close this out message jids equals errors get to do name and we'll just give this an M22 okay let's take a look bam okay there we go to do name field must be at least three characters that's just a little helper function that larel gives out of the box to say okay anytime we pass in that validation errors so if I to create more we're creating we're not actually doing anything with that function yet because we're still we
just validated it so now that we validated that we can use the to-do name so we're going to go off user todos because we created that helper function we're going to create um and we're just going to say the name of the too is this to-do name that's already been validated here should be we should be good to go in the Box let's take a look so if I was to come back here and say first to-do I'm going to press enter and automatically we have that to-do sh
because we have that four each that's kind of been scaffolded out so if I create a second one second one okay this isn't clearing so let's go ahead and fix that live wire actually has a new U method out of the box instead of passing in the D name we can actually say this pull to do name and now what this is going to do is it's going to grab the name push it into name and then just Straits it so basically if we were to go
back and we were to say third one okay we're clearing it perfect let's go ahead and maybe we'll just tidy this up a little bit and we'll say classes Flex okay and maybe we'll just say items Center just for fun and we'll go like SpaceX 4 there we go and we'll do some space y here Space y4
2 there we go okay not as great as it could be but it's it's something we have we have to do now let's go ahead and add one more thing because we can delete them well we can because I actually don't have a a method created yet but let's go ahead and add that so we have our create Todo and now we'll go ahead and add a public function of delete and I think we called it delete
but we why don't we do delete to do we'll pass an integer of ID and we're just saying Au user to-dos where the ID that we're passing in his ID let's go ahead and delete it so if I was to go ahead and update this where this button I can say delete to-do and we're passing the to-do ID here okay this is great this should work delete delete delete there we go first one still works perfectly but there's a problem with
this there's a security issue because anyone can technically now delete that to-do because they can just pass in that ID in the front end and then just go ahead and delete it so what can we do well lvo provides policies which is kind of of um out of the box like authorization saying okay is this person able to actually delete this to do and what we can do is go ahead and do PHP artisan make policy and we'll do to-do
policy and pass in the model of Todo there we go so on the to-do policy let's see what we can do well we have the to-do and the user model already given to us with a bunch of these functions we really only care about the delete one right now so we're going to say is return the user ID uh that is equal to the to-do user ID so this is just
saying I only want this person to be able to delete this to-do if their ID is equal to the if they own that to-do so let's go ahead and go back into our to-do manager now the only thing we have to do because Lara gives us us out of the box is just say in in delete to-do I want to Au authorize this first so um I want to uh authorize and I want to
authorize delete and then I want to authorize the to-do and in this case we can do like this we can do to-do equals um off user todos find and we can pass in this Tod do here um there we go and then we can go ahead and delete that Todo this so this is just saying okay I'm grabbing the to-do based off the ID that's being passed in I'm saying is this person
actually authorized to delete this to-do and then if they are we can go ahead and delete it so let's see if anything changes in our code it should not so if I was to say second one delete okay we can still delete it and let's do fifth one perfect and now just to make sure that we're passing in because when we pass this with we're only grabbing the users to do let's go ahead and create a new account see what this looks like l
out let's go ahead and register we'll just say this is Ben Smith Ben test.com perfect and Ben doesn't have our to-dos so Ben has his own to-dos perfect need to delete them as well and there you have it we have a to-do list that's been created within you know 15
17 minutes with authorization so and it's specifically segmented to the authorized user specifically segment to the person who's actually authenticated and user accounts are kind of given to you out of the box so hopefully this gives you a good head start on what it's like to build with Lille what it's like to build with L Live Wire and what the next best step is for you as you learn of all the things Lille has out of the box you can start implementing emails
and cues and jobs without having to leave the framework so we'll see you next time