Passing Arrays to Views0:05
All right, let's do four or five minutes on blade directives. So imagine you need to send through a list of tasks to your view, right? Maybe something like this, tasks, and I will paste this in. So to start, we'll have an array of strings, like go to the market, walk the dog. So at this point, we know that this key will be extracted into its own variable, right?
that this key will be extracted into its own variable, right? So within my view, well, PHP 1 0 1, you can't do this, right? So we are effectively trying to echo an array and that does not work. So we're gonna see an error here. Notice we see what the error was, as well as which line the error occurred on. Okay? So yes, of course you can't pass an array
Debugging with dump/dd0:42
as which line the error occurred on. Okay? So yes, of course you can't pass an array to h tml special cars. Alright, so in the past, maybe what you did is something like this. You would open up PHP and you would say, VAR dump. And then you provide your data, right? If I come back, give it a refresh. Now you can inspect it and you can still do this. Or maybe what you did is this number here
Now you can inspect it and you can still do this. Or maybe what you did is this number here where you would say, die and then var dump. All right? I just need to see what's going on here. And then kill the execution. Well, as it turns out, this is what we do in, in Laravel apps as well, but we do it in a much more TSE way. So things like this, it's just a little bit verbose. And remember, we're using Blade here, which
So things like this, it's just a little bit verbose. And remember, we're using Blade here, which as a reminder is like a templating engine on top of PHP. But the key here is ultimately that template, it just compiles down to raw PHP and that's what gets served. So we can add a little bit of sugar to make things like this. Um, just a bit easier to write and prepare. So instead, if I wanna dump something to the page,
Um, just a bit easier to write and prepare. So instead, if I wanna dump something to the page, I can start with the at symbol. And that's how you can spot a blade directive. And then you type in the name of the directive, in this case, dump tasks. Now, if I come back and refresh, I effectively get the exact same thing. So notice if I have something after it, like a heading, well, I'm still going
So notice if I have something after it, like a heading, well, I'm still going to see the heading because we're simply dumping something to the page, uh, temporarily. However, if I do need to die and then dump, well we're gonna reach for dd. And you have been invited to the Laravel community and we love DD in this community. So you might even wanna buy a shirt that says DD on it. Sure enough, now it dumps it, but it kills the execution.
So you might even wanna buy a shirt that says DD on it. Sure enough, now it dumps it, but it kills the execution. Uh, so now we don't see the heading at all. Alright, so already, you know, two blade directives, but actually, you know, an endless number of blade directives. Uh, any, anything somewhat tedious you did in PHP has almost certainly been replaced with a blade directive. So for example, let's say we wanna check, um, if we have any tasks.
Conditional Blade Directives2:43
So for example, let's say we wanna check, um, if we have any tasks. Well, in the past you might do something like this. Do we have any tasks? And you did this shorthand, right? Um, then you'd say, and if, right, maybe now we'd say, yes, we have some tasks. How many? And then we could say echo count tasks like this. In fact, you know, you get the idea, uh, it's fine. This works. You can still do it, it's just, I dunno, it's a little hard on the fingers.
This works. You can still do it, it's just, I dunno, it's a little hard on the fingers. All of those angled brackets. So if I come back, uh, excuse me, if I come back and refresh, yes, we have some tasks, how many three tasks and facts, um, yeah, it works, but I don't like doing this. So instead I'm gonna swap this out with a directive. And you guessed it, it's at symbol if, if we have any tasks, what about down here? And if, well, we have a
what about down here? And if, well, we have a and if blade directive, like so, alright, let's come back, give it a refresh. And yeah, we still get the exact same thing, but I'm sure you can agree this is much easier on the fingers to write and it's also easier on the eyes to see. Now again, just remember, ultimately this is being compiled down more or less to what we wrote before.
ultimately this is being compiled down more or less to what we wrote before. Okay? Now, if we inspect these, uh, if you have a good IDE, if you type the app symbol, you should see a bunch of directives. And again, it can be a little overwhelming at first. Don't worry about it, just reach for the ones that you know about. Uh, and an endless number of these you'll never reach for and I have never reached for.
Looping with foreach4:13
Uh, and an endless number of these you'll never reach for and I have never reached for. So yeah, you can do if, if you wanna do, if not, then you could use unless like you might see in the Ruby community again, ultimately that compiles down to PHP. If not, right? If you need to loop over something, you can reach for four. If you want a while statement, you can use that. If you want a four each, you can do that. So for example, I could say for each tasks as task,
If you want a four each, you can do that. So for example, I could say for each tasks as task, then let's close it out and that declares the end of the block. And then here we can echo out the, the string task itself. So if I come back, sure enough, I see our three items and if we inspect it, sure enough, they are wrapped within list items as you see here. Cool. Uh, what about situations where the array is empty?
Handling Empty Arrays4:56
Cool. Uh, what about situations where the array is empty? Uh, maybe we're fetching this from the database and there are no tasks at the moment. Well, if I give this a refresh, we don't see anything, right? So in the past what you might do is say like, well PHP, if not count tasks, close this out. Then there are no current or active tasks, right? And that would work and you can still do that.
Then there are no current or active tasks, right? And that would work and you can still do that. Um, you know now that instead of, if not, we can optionally use unless, so I could update this and just make sure you update the closing directive as well so they match up. And if I were to come back, we're still gonna get the same thing, but because this is such a common pattern, instead we could reach for, uh, a four else blade directive.
because this is such a common pattern, instead we could reach for, uh, a four else blade directive. And that takes this shape four else. So this has the same API as for each, uh, loop over the task, display it. But in the event that we don't have any at all, we can say if it's empty, then display a paragraph. All right? So now what we have here is functionally equivalent to what we wrote before. Come back, refresh. There are no active tasks,
equivalent to what we wrote before. Come back, refresh. There are no active tasks, but if we return it to what we had before and reload, now we loop over them. Cool. So yeah, if you have just a basic familiarity with, um, rendering data within a PHP view, then it's not gonna be any different here. You just now have an extra layer of convenience and that convenience is available through blade directives. Now what you'll also find,
Auth and Authorization Checks6:35
and that convenience is available through blade directives. Now what you'll also find, and this is a little beyond where we currently are in our learning, but there are things like off check if we have an authenticated user check. If the current user is just a guest, meaning it's somebody visiting our site, but they haven't yet signed in, they're not yet authenticated, we could check for authorization.
but they haven't yet signed in, they're not yet authenticated, we could check for authorization. Can this user edits the post? We might do something like this if we had a variable in this case, notice that, um, it's not gonna work because there is no post variable. But nonetheless, we could say, I don't know, have an anchor tag that goes to posts edits and we can render an edit link only on the condition that the current user can edit this post.
and we can render an edit link only on the condition that the current user can edit this post. And again, all of this is available through these blade directives. That to reiterate, compile down to vanilla PHP expressions.
