How To Create Your Own Form Request In Laravel | Laravel For Beginners | Learn Laravel

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up guys my name is dari and i hope that you have a great day because today i want to talk about form requests because of you guys i've reached something very special that i never ever dreamed about reaching in one year this channel got over 5k subscribers in 2020 and i can't thank every single person that watches my video enough therefore i want to do a simple 50 amazon gift card giveaway and there's not a lot that you need to do i recently created an instagram account which i have linked in the description down below and this is not a sponsored giveaway by amazon and this has nothing to do with youtube i just want to give something back to the people that supported me the only thing that you need to do is to follow me on instagram write down why you think that you should win the giveaway on my latest post and if you have any ideas of a giveaway item that i could do in the future just write it down as well i don't need any personal data whatsoever and the giveaway will end on the 8th of february in the last video we talked about form validation but as you start to build out your own application you probably start noticing some repeated patterns in your controller the validation that we're doing in our store method so validating the name founded in description could be copied we could scroll down to the update and place it right above where we update our car and as you might see we're repeating the same five lines of code twice in our application and it could be way more and later on you even need authentication for some methods as well so you will repeat yourself there as well if your goal is to structurize your code better and extract these common behaviors that we have inside the controller you may be interested in larval's form requests and to do this we need to create a form request from the command line so let's hop to the cli right there let's say php artisan make me a request followed by the name of the request so let's say create validation request let's hit enter and our request has been created successfully so a form will be created for us and to find it we need to go back to our code inside our app folder you can see our http and a new folder requests if we open it you can see the create validation request form that we just created now in here you can see two methods authorize and rules there are both public which means that they are available from anywhere inside your code so let's focus on the authorized first because it's an optional method if this method returns true so let's replace false with true this action is authorized to perform this request if it's false it will be rejected and in our case we need to set it equal to true the second method that we have is the rules and this is actually pretty self-explanatory it will check some rules before it will perform so what we need to do is to return well we could go to our cars controller copy our name founded and description inside our validate paste it right here let's remove new uppercase to requires save it you might be wondering how we could use our form request and this is where laravel's magic comes into play any route that type hints a form request as one of its params will benefit from the definition of that form request so what we need to do in our controller is to go to the top right below our use create a new use app forward slash http forward slash requests forward slash create validation request save it let's go down to our store method instead of creating an instant of the request object right here we can change it to create validation request as one request now what laravel will do is validating the input and authorize the request since we've set it to true inside our create validation request right here so we will authorize it if the input is invalid it will act just like the request object that we had before so inside our store method we could basically say request validated you could actually set this equal to a variable so let's say validated equal to request validated if you want to see the output so you could dd it but in our case let's just write it down like this let's remove our entire method that we have because it's not necessary anymore because we're already validating it save it let's go to the browser refresh it first create a new car mercedes 1918 mercedes well let's actually change the founded to a string so we know if we're getting an error message or not submit it and we have been redirected to the previous page with the appropriate error message passed along right here later on if the user is not authorized laravel will return a four or three forbidden error message so what we could do right now is to go to vs code copy our request validated let's go to the update remove the five lines of code paste it right here change the request object to create validation request and this works as well this was it for this video where i showed you how you could make your application well especially your controller better readable if you do like my videos and you want to see more leave this video a thumbs up and if you're new to this channel please hit the subscribe button
Info
Channel: Code With Dary
Views: 9,277
Rating: undefined out of 5
Keywords: laravel, laravel 8, laravel php framework tutorial - full course for beginners 2020, laravel 8 tutorial for beginners, laravel php framework tutorial full course for beginners, learn laravel for beginners, learn laravel step by step, laravel full course, php laravel youtube, laravel tutorial youtube, how to learn laravel, laravel tutorial 2020 - the complete developer course, laravel tutorials from scratch to advanced, laravel form validation, laravel form request
Id: wNnLnN3nubc
Channel Id: undefined
Length: 6min 2sec (362 seconds)
Published: Wed Jan 27 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.