Accessing The Request In Laravel | Laravel 8 For Beginners | Learn Laravel 8

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up guys my name is dari and i hope that you're having a great day because today we're going to talk about how we could handle user data because of you guys have 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 enough therefore i want to do a simple 50 amazon gift card giveaway 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 or youtube i just want to give something back to the people that supported me over the last year the only thing 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 idea of a giveaway item that i could do in the future just write it down as well i don't need any personal data or whatsoever and the giveaway will end on the 8th of february alright let's dive into the actual content of this video this is a topic that i want to show you just by showing you some examples later on when we're going to create a complete application you'll be seeing how we could use these examples in the best way possible what we've done so far was pretty simple we just did some requests to the database but if we take a look at our migrations let me open it let's go to any migration it really doesn't matter we have integers string timestamps foreign keys websites that use a framework such as laravel aren't often used to show static content usually these websites deal with complex and mixed data sources think about user input from forms url paths queries posting data and way more we have created a small application as you can see right there where we pretty much do every little performance we create it we print it out we update it we delete it now laravel provides a collection of tools for gathering validating normalizing and filtering user data and that's what i want to show you if we take a look at our cars controller so let's open it let me scroll up and let me zoom in alright this is good you can see that we have an index method to show the data we have a create method which will show a form and the method that i want to focus on right now is the store method that we have right here well let me actually zoom in one more time the reason why i want to use a store method is because of the request that we have right there inside our method up until this point i really haven't talked about it and i remember that i said that i will talk about it later on and the moment is right there now the request instance that you see right here is a global helper from this specific request instance we can access loads of other methods or options and each of these options exposes the entire illuminate request object right here there's way more available than only user data but in this video i want to focus on the methods that are specifically related to user data so what we've done so far was specifying every single input field right here and then we created a new car right here and we pushed it right into our database we could make this a lot easier by basically saying request access operator all so the all method just like the name suggests the name all this method gives you an array containing all of the inputs the user has provided so basically what we're doing right here honestly i don't really recommend using this way when you're creating a post request but it's something i personally think that i have to show you now to show you the actual data let's set it equal to a variable let's say test and let's go on the line below and let's say dd variable test now in order to show the actual output we need to add a new car because we want to store it so the brand name is let's say mercedes founded in 1914 and the description is this is my mercedes let's click on submit and you can see an array with four elements inside of it the first one is our token as you could see right here and this token is basically the csrf token inside our create form that's being passed through we have the field name founded and the description which is all right so next to the all method well let me actually add a comment request all input fields now let's comment it out so next to the all method there's also an accept method so let's say accept method and this method will provide the same output as the all method but you can choose one or more fields to exclude right now as you could see in the browser we're adding our underscore token and that's not something that you usually want to submit or to show a user so let's go back to our code editor and once again let's say variable test is equal to request and that's called the accept method so in here we could pass in a name as a string so let's say underscore token so what we're basically saying is show me everything except the underscore token save it refresh the browser and you can see that name founded and description is being showed and the underscore token has been removed and before i continue on to the next method this could also be passed in as an array so let's say that you want to exclude more than one value so let's get rid of the content insider method let's say brackets single quotes underscore token second param let's say name save it go to the browser refresh it and we have excluded two fields as you might have noticed in the previous videos there's almost always an option to inverse something in laravel and that can also be done for the accept method so to do that we need to replace accept with only so we're basically saying show me only the underscore token and the name save it go to the browser refresh it token and name has been shown to us and this could also be done as a string let's get rid of the brackets and only show me underscore token refresh it and the token has been showed all right sometimes you want to check user data to see if it's available to you and that can be done with the has method you might want to check if a specific user input field has been entered so let's comment out this line of code that we have right here so let's add a comment as method let me scroll down all right so let's say variable test is equal to now before we continue on you need to remember that this will not filter through our database to find a row with the name of whatever this will check if the current request so the request that we're passing in right here has a value that we're passing in inside the input field so let's say the request has now let's pass in let's say found it so we want to find the value or we want to see if our founded has been entered save it refresh the browser and true has been printed out because it has been entered now usually you don't use it like we're doing right here what you usually want to do is to well let's say if request has found it then let's say dd founded has been found save it go to chrome refresh the browser and found it has been found has been printed out on the screen next to everything we've done so far it's also very easy to get the current patent information so let's comment out everything that we have let's create a new comment let's say current path and this could actually be placed inside the dd and we want to dd the request path method save it go to the browser refresh it and this will return the endpoint of our uri so in our case it's forward slash cars as you could see right here now you might wonder why i'm showing this to you well this is because it will allow us to verify the incoming request path so what we could do is to get rid of our dd we could basically create a new if statement and in here we could say request is cars so if the request or the endpoint is forward slash cars let's say dd end points is cars let's save it go to chrome refresh the browser now you can see that this is true because endpoint scars has been printed out and it works fine all right let's hop to visual studio code let's actually comment it out again let's say that we want to talk about the current methods now sometimes you want to double check the method request therefore we could use the request method which will return the http verb for the request now inside our method we need to pass in the method request so let's say post and the best way to show this to you is to add a if statement again so let's say if our request method is post which is true because we're posting a new car dd method is post method without a capital e refresh it and method this post has been printed out on the screen you could also check whether it matches the specified verb and in that case we need to change method to is method save it go to chrome refresh it and the output remains the same let's go to visual studio code commented it out again and let's say that we want to show the url sometimes you want to retrieve the full url with the query string so if we say dd and in here request url save it refresh the browser the output will be the entire url so what we would have done in php was basically defining the url root as a constant but now you could do it with the request and the last method that i want to show you is let's comment it out actually my bad how to show the user's ip address so show the ip now what we need to do is to create a dd inside our dd request ip so find me the ip and this will retrieve a user's ip address which is used to do the request to your application save it go to chrome refresh it and since we're running on our localhost we will get 127.001 but whenever you do a request on the server you could save a user's ip address alright guys this was it for this video where i've showed you a couple cool things you could do with the request method that laravel provides for us in the next video i want to talk about something very cool and that's validation in laravel 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 that subscribe button
Info
Channel: Code With Dary
Views: 2,444
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, accesing the request in laravel, request in laravel
Id: e-2t2-oMQqg
Channel Id: undefined
Length: 12min 12sec (732 seconds)
Published: Mon Jan 25 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.