So if you've not heard of the optional helper before, let's just go ahead and die dump on optional and let's just go ahead and pass in a null value to this and try and access some property from this. Head over to the browser, sure
00:13
enough we see null. So essentially let's say that you had a user's address like this and you wanted to go ahead and extract out line one from this for example. If that doesn't exist we still get a null value so it's just a little bit of a nicer way than doing a ternary. Now the update that we have in optional
00:33
is that we can pass an optional callback to this. So just before we dive in let's go into the source code. So let's open up the helpers file just here and come down to optional and you can see that we can now pass a callback into this. This goes ahead and checks if the callback exists. If it doesn't it just works in exactly
00:51
the same way. Previously the code for this would have just looked exactly like this and otherwise what this will do is it will return with the callback and the value if the value that you've passed into here is not null. Now this isn't a callback that allows you to specify a default value. Instead it's more of a
01:10
safety net to say well if that particular thing I've passed in does exist then I want to do something with it. It's the equivalent in other languages and patterns where we have optional something and then we have something like when. So when that's available we can do something with it. Now just as an
01:25
example let's just bring up the database here I've got a user just in here that I've generated and I don't have an address. Let's just imagine I'm signed in as that user and I want to go ahead and update a previous existing address. Now the thing with this is this is a has one relationship so it won't return a
01:42
collection it will return a null value if that address does not exist. Now previously what we would have to do is create an if statement which is absolutely fine there's no you know reason that you wouldn't create an if statement but you know we are gonna look at a slightly tidier way to do this
01:58
which isn't for everyone. So let's go ahead and check if the user doesn't have an address if they don't already have an address we're gonna go ahead and return because we don't have an address to update and then what we're gonna do is come in here and say request user address because now we know that exists
02:12
and we're gonna go ahead and update that model so we can just pass in anything inside of here and let's just go ahead and update the line one so let's say 1 2 3 street now of course if we run this in the browser that's just not gonna do anything because we don't have an address already let's just come over to
02:28
the database and manually add some address in here with that user save this out and let's give that a refresh sure enough that's been updated now what the optional helper allows us to do if we just get rid of this for now is get rid of this if statement altogether so let's get rid of that let's pull this up
02:46
and let's go ahead and give this a refresh so sure enough we get an error here we're trying to call an update function on null value because this does not exist now previously what we would have to do is if we just wrap this in optional here is something like the following now will this work let's give
03:03
that a refresh and sure enough it does but what if we wanted to do something slightly more complex than update this let's just say that we had a couple of things that we wanted to do with this address perhaps fill it with some data and then save it this would be a little bit awkward so for example we could fill
03:20
that like this and then down here we could go ahead and assign this value and it all gets a little bit messy and this is just one way of using it as soon as you know that this actually exists and exactly how it works then you can use it depending on the specific problem you're trying to solve so let's just take this
03:37
update statement here and instead go ahead and pass in this callback and this callback like we've seen before will only be run when that particular value does actually exist so now what we can do inside of here is pretty much anything we want so for example we could say address update and we could go ahead
03:54
and pass in say from the request just the line one or just any information that we want to take from this so let's go over and pull in the request into scope here and let's just go over here and add line one to our get just here and that's going to go ahead and update only if that user has an address so
04:13
let's just add something in here once again and let's go ahead and update this to something slightly different so one two three Street for example and that's going to go ahead and work because we know that that address model exists but like I said if you were doing something like filling this and then going ahead
04:27
and saving that address that's going to go ahead and work in exactly the same way because you have a callback you have the ability to put in multiple lines we'll switch this up in just a second to see how we would do this without the optional helper new feature so if we give that a refresh sure enough that
04:44
works and let's just change this over to code Street and sure enough that works as well okay so to do this without this what we'd have to do is of course take this out of the callbacks we wouldn't have that available and we would have to say something like address equals optional address we couldn't then do
05:01
this we would have to introduce an if statement in here to say if the address is not there then we return so we could use defensive programming or we could of course take away the faulty check and put all of this inside of here so this is the equivalent to using the callback let's just go ahead and update this one
05:19
more time just so we can see what we're doing and sure enough that's been updated whereas if the address doesn't exist of course this still doesn't break because we have this check in here so moving this into the callback makes sense if you have a problem like this really really straightforward pop that
05:35
callback in there pop whatever you're doing with that inside of there bring in the model that you now know exists and of course bring anything in here into scope that you need and that's going to work as normal but of course it's a lot tidier
1 episode• 5 mins•6 years ago
Overview
You may have used the optional() helper in the Laravel framework before, but did you know about the optional callback?