Playing
15. Accessing request data fluently

Transcript

00:00
There's a bunch of ways that we can access request data within Laravel,
00:03
data that we have in the query string or that we post or patch down into a form, whatever we do. But we have a new fluent method and I'm going to show you the difference between some of the others why this might be useful. Okay so let's go ahead and just bring in our request object just so we can dump out some data here. So let's pull this in from Illuminate HTTP request
00:26
and of course we're doing this within a web root but you typically do this within a controller. Okay so if I dump out request and name, of course if we come over here we get a null at the moment but we could go ahead and add a name in here like this and we get that output. Now if you've been working with Laravel for any length of time you'll probably already know this. What we can also do
00:48
though is say request all. We can say get and get individual properties but we can also say request all. That returns an array so whether we have this in the query string or we're posting this down through a form we get this as an array. Now arrays are fine but they're not the best thing to move around your application. We're going to look at an example of that in a moment but let me just show
01:11
you a couple of other ways that we can grab this data. So another way is using the collect method which is less common but gives us out a collection of this data. Now that is convenient but again if we were say passing all of the request data into an action or we were putting this into a job to be queued or something like that it's probably not the best thing to pass in. Now one of the
01:35
reasons for that if we just go ahead and access this data here is that collections are good for manipulating data and then returning say an array or something like JSON but they're not so great for just storing some basic data and then plucking data out and it's also really hard to modify the data inside of them as well. So to give you an example of that if I say data and name we would
01:57
kind of expect that to work but it doesn't quite work like that because we're trying to access the name property on this collection and it doesn't map it up for us. Instead what we would need to do is say get a name specifically and that really muddies our code up. So let's go ahead and take a look at what the fluent object is within Laravel which you might not have heard of before today
02:21
and we'll see what this new fluent method does. So let's just take a bunch of data I'm not going to take this from the query string I'm just going to go ahead and hard code this in here so let's say Mable and we'll set just another property in here like age can be any data type. Now of course if we go ahead and dump this out we would expect to get an array that's exactly what we've just
02:42
built up here but now let's go ahead and transform this into a fluent object. To do this we just new up the fluent object in Laravel which comes from illuminate support fluent and we just pass that array data in. Let's go ahead and dump this out and see what we get now. So let's dump out this data and give that a refresh and it kind of looks like a collection but there's a big
03:04
difference here. This fluent object allows us to first of all very easily access properties from this array so I can just say data name now and that gives me the name it gives me stuff like the age so I can do that as well. It works kind of like a collection but it's a lot more convenient. Now what we can also do or the other benefit of using a fluent object is that it's null safe. Now what
03:28
do I mean by that? Well let's say we'll just get rid of this for now. Let's say we were dealing with an array of data perhaps we'd use the all method to grab all that data from our request. Well if I wanted to go ahead and grab out say another property of this so let's say location what's going to happen here? Well because this is an array obviously is a primitive type in php
03:53
when we give this a refresh we get an undefined array key error so that's not great so this is what we call non-null safe. Now when we're talking about a fluent object this is null safe so if I go ahead and try and access the same piece of data from here using a fluent object behind the scenes it's checking if this actually exists within that array and if not it returns a null so this is a
04:17
much more convenient way to work with data particularly where you may have nullable data. Now without getting too far into the fluent object it also has a bunch of other features that we can use like the ability to set something so let's just go ahead and use the location method which might seem a little bit strange but what we're doing here is we're fluently setting the location
04:38
key of this array to the value north when we go ahead and die dump on this data we would expect to see three attributes now with our location set and there are a bunch of other stuff that we can do with this as well so for example we can very conveniently cast this to json if we were to output this in some sort of api so there are a bunch more things that we can do with fluent but
05:00
the whole purpose of this video is to demonstrate that now what we can do is we can access request fluent and that will give us back you guessed it a fluent object with that array that we initially saw when we used all so if we give this a refresh now you can see we get that fluent object with the data in the query string or the form that we've just posted through and now we have a nice fluent
05:25
object that we can manipulate and is also null safe so we can pass it around our application and we can pretty much work with it like we would with any other object that we pass around so as an example let's go ahead and create out a directory in here called actions i typically use actions within laravel applications i find them really nice to work with so let's go ahead
05:46
and just create out a class in here called create user we'll just keep this really really simple but you should get the idea so in here we might have a handle method that takes in that data so in our case if we were passing an array here and then using something like user create we're obviously going to end up with potentially stuff that didn't exist so we'd have to check this with
06:07
is set or something you know it's not the greatest way to do things but it gets a little bit cumbersome but now that we've got this fluent object which we know is slightly better than a collection for this kind of stuff we can go ahead and type in that if we want to in here and we can go ahead and do something with that data for now i'm just going to go ahead and dump out the name
06:27
just so we get the idea but you can go ahead and fill that in with anything you want so now that we've got this we can validate the data and then we can go ahead and new up a create user action and let's just get rid of that really quickly and we can go ahead and handle this and now we can pass in request and fluent and we have a nice fluent object that we can work with and of course
06:49
at the moment our action just dumps out mable so this isn't to say that you should always use a fluent object it depends on what you're building and how you're working with your data you might specifically be using data objects that you build up yourself or via a popular package to pass this data around but really the next best thing is a fluent object this just allows us to pass this
07:12
data around effortlessly we get null safe protection and we can access properties kind of like we would in a data transfer object by just accessing the property name so next time you have a bunch of data either through your query string or when you're posting a form you now know that you have that fluent object which if you didn't know existed before now you do
26 episodes2 hrs 34 mins

Overview

Need to know what’s new in Laravel as it happens? Every episode of this course is dedicated to covering the most interesting and useful Laravel additions in detail, so you’re ready to start using them in your applications.

Check back often, and stay completely up-to-date with Laravel.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!