Playing
06. The when() helper

Transcript

00:00
The new Laravel when helper is particularly useful inside of your blade templates, whether that be just a standard blade template or within Livewire, but can also be used on its own. So first of all, let's take a look at the syntax and then we'll look at a few practical examples of where you might use this. OK, so to start with, let's go ahead and boot up a tinker session and let's look at how we actually use this. So as you can imagine, it's just a when function.
00:25
Now into the first argument, we're going to pass in a boolean value. So, of course, true or false. And then the second argument is just going to be either a string or any other value or a closure. So if I run this, you'll notice that we just get hay out.
00:39
If we try that again, but with the false value in here, sure enough, we get null. And we can also pass a closure in here, either a short closure, anything that we want to return. Let's switch this over to true. And there we go.
00:52
We get hay returned from whatever's within that closure. So this at first glance might not seem that useful, but let's go over to a blade template and look at how this is used. So let's go ahead and create out a div in here. And let's say that we want this to be content editable under a particular condition.
01:12
So inside of this div, let's just write hay. Let's go over to the browser and give this a refresh. And sure enough, I can edit the content of this. If I wanted to make this conditional, I could actually use the when helper directly in here to output this attribute.
01:26
The first way that we would do that is to use an if statement. So you would say something like if true, then we would want this to be content editable. Otherwise, we would just do nothing and not show the content editable attribute. So as you can imagine, that still works.
01:41
But with the when helper, this is a little bit more convenient. So what we can do in here is we can directly use the when helper, say true, and then we can just directly pass the content editable value into here. So if we hop back over, sure enough, this is editable.
01:57
And if we change this to false, then that goes and we can't edit that content. Before we move over to the live wire example, be really careful with this because we actually already have a bunch of built in blade directives that we can use to do things like set something as disabled. So, for example, if we wanted to mark this particular item as disabled, let's just say this is an input. And let's get rid of this and switch this over.
02:24
We can actually just use this disabled attribute passing in this value. So you can see this is now disabled. And of course, when we set this to false, it's enabled again. So, for example, we wouldn't do something like the following and use when here in place of disabled to output the disabled attribute like this.
02:44
There's no point in doing that. So just check if there is a directive before you go ahead and use when. OK, so now that we've looked at this, let's take a look at a live wire example, which is where this can be incredibly useful. So I'm going to go ahead and look at an example of listing through a bunch of users.
03:01
But if we have some sort of condition set to true, like we're an admin, we're going to make the users clickable to invoke a live wire action. So let's go ahead and make out a live wire component. And we'll just call this user index. Let's just hop over to Tinker just to make sure we seed our database with some users really quickly.
03:20
Let's do this for 10 users and go ahead and create them. And then let's register our route here. So let's go to our web routes and we'll create a get route here to go through to users. And we'll use that user index component.
03:35
And let's open this up and just apply a layout to this. Let's say layout and we'll say layout.guest. We'll just keep this really, really simple. So when we render something out, let's just go ahead and output all of the users in our app here.
03:49
And let's go over to the user index and just iterate through them. Just keep this super simple. So for every user, just go ahead and dump out the user's name. Let's just make sure that we pull in the namespace for user.
04:06
And let's head over to our app, over to users. And there we go. So what we want to do is we want to allow these to be clickable. Probably not the best idea, but you'll get the point of this by the end of the video.
04:19
We want to allow these to be clickable, but only under a certain condition. And by clickable, I mean we want these to invoke an action. So, for example, let's say over in our user index component, we had a method in here which would launch a modal. So let's say launch modal.
04:35
It might be an edit modal or something like that. Let's just die dump and say launch edit modal. And I'm going to change the name over just so it's a bit clearer. So when we apply this to each of these items, so let's just apply it to the div for now,
04:50
we would say wire click launch edit modal. So pretty straightforward. Click on that. That will launch a modal for us once we've implemented that functionality.
05:00
But what if we want this to be conditional? Well, in our case, we could do an if statement in here and say, well, if we are an admin or something like that, then go ahead and output this attribute. But you'll find that these get a little bit messy.
05:13
So this does work. We can still click on this. But now we can actually use the when helper to make this a bit cleaner. So imagine that you've got a bunch of stuff in here and you just want to make this line that little bit cleaner.
05:26
Well, we can use when for that. Now, there's a slight difference here in the way that it's going to work now that we've got these wrapped in double quotes. So let's take a look at the difference. So let's go ahead and say when true.
05:38
So just imagine that some sort of check for whether an admin or not. We can just paste this directly in here. Now, this isn't going to work at the moment. If we click on this, you can see that by opening up our console, we've got an error here with an unexpected string.
05:50
That's just because by default, this will get escaped. What we can do, though, is switch this up. So we use a raw output instead. So let's go and just get rid of one of them curly braces, raw output this.
06:01
And as we head over and give this a refresh. Sure enough, it works in exactly the same way. So you could combine this if you were allowing a div to be clickable with the class helper. So you could go ahead and output a cursor of pointer if the same condition was met.
06:19
And that, of course, would change over the UI. Let's go ahead and run npm run dev really quickly and check this out. And as you can see, now we've got a pointer for each of these. Whereas if the condition, like if the user was an admin, was false, then that would just render out a normal list for us.
06:36
So this can be really, really helpful, particularly if you're working with something like Livewire. And you need to output things like something needs to be clickable under a certain condition. Or you want to pull something under a certain condition. The when helper nicely cleans this up.
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!