Passing Data to Views 0:00Welcome back! OK, let's figure out how to pass data to our Vues. So, I will open my routes file. Or, of course, remember, if you're using any editor like Sublime, or basically any modern editor at this point, make sure you do shortcuts. So, in the case of Sublime, Command-P, routes.php, and you're there. Anyhow, in the last lesson, we returned a basic string, which Laravel converts to a response. But yeah, in real life, you will be loading Vues, like this. So, imagine that you have some data you want to display on that page.But yeah, in real life, you will be loading Vues, like this. So, imagine that you have some data you want to display on that page. And maybe this is coming from a database, or a cURL request, or anything of that nature. OK, so we will fetch the data. In this case, I'm just going to use a simple array. And let's do the hosts of the Laravel podcast. So, Taylor, Matt, and myself. Now, we can pass data to our Vues in one of two ways. First, as the second argument, we can pass an array here. So, for example, I could say, within my Vue, I want a variable called people, and we'll View Data Passing Options 0:52First, as the second argument, we can pass an array here. So, for example, I could say, within my Vue, I want a variable called people, and we'll make that equal to this. Or, as you may know with PHP, if you do something like this, that will achieve the exact same end result. The compact function will create an array with a key of people and the value equal to whatever is associated with that variable. So, that works. Another option would be to use this with method. So, for example, I could say, with people equals people.Another option would be to use this with method. So, for example, I could say, with people equals people. One final option is, we can use what we call dynamic methods. So, in this case, I want to pass through a key of people. So, I could also do something like this, and Laravel will properly parse it. And yeah, either of these approaches are fine. There's no right or wrong. Just try to be somewhat consistent. In our case, why don't we do that compact approach? OK, so this says, load a Vue called welcome, and I want to pass to that Vue a variable Looping Data in Views 1:43In our case, why don't we do that compact approach? OK, so this says, load a Vue called welcome, and I want to pass to that Vue a variable called people and make that equal to this. So, now we can go to resources, Vues, welcome, or once again, command P, welcome.blade. Much faster. Now, if we scroll down, let's filter through and display those people. Now, first, we'll tackle this with vanilla PHP, the basic standard way, and then I'll show you how to use Laravel's templating engine called blade to tackle this. So, yeah, if you come from the WordPress world, you might do something like this. For each, people as person, and for each, yeah, we've done this thousands of times,So, yeah, if you come from the WordPress world, you might do something like this. For each, people as person, and for each, yeah, we've done this thousands of times, right? And then maybe here, we'll echo out the person's name, like so. All right, let's boot up a server, php artisan serve. And if you have iTerm installed, you can command click on the URL. All right, and sure enough, in the top left corner, we can see the output here. But now, what about this blade approach? You know, in the past, we always had to write code like this in our views. But yeah, it's kind of gross to look at. Converting to Blade Syntax 2:49You know, in the past, we always had to write code like this in our views. But yeah, it's kind of gross to look at. PHP is a templating language, but it didn't really evolve to become an overly good one. So instead, we're not going to use Laravel's blade engine. Here's what we do. I can get rid of all the PHP tags and replace it with at symbol and then the name of the keyword, in this case, for each. Next, I can get rid of that entirely because Laravel, behind the scenes, will build up the PHP fragment itself. And that's the way this works.the PHP fragment itself. And that's the way this works. Our views are loaded, they are compiled, they are cached. And then that cached file that contains generic PHP is actually what gets loaded. OK, and now down here at the bottom, exact same thing. Replace this with at symbol and then the keyword. Finally, for echoing out data, we can replace that with two braces like this. So now we have more or less a direct conversion to blade. And if I were to switch back and refresh, of course, we get the same thing. Now, what you'll find is it's not that much more complicated than this. Blade Conditionals and Helpers 3:48And if I were to switch back and refresh, of course, we get the same thing. Now, what you'll find is it's not that much more complicated than this. You will learn about layout files in a future lesson. But generally, blade is very simple to use because it piggybacks off of what you already know. So, for example, a simple if statement, if maybe empty people. Once again, you don't do this. Laravel will do that automatically for you. So you can simply designate it with opening at if and then you close it with end and then the keyword.So you can simply designate it with opening at if and then you close it with end and then the keyword. OK, so if it's empty, there are no people. Come back, refresh. It is not empty. So, of course, we don't see it. But we can fix that. Let's empty this out. Come back, refresh. And now that works.Come back, refresh. And now that works. Now, the exact same thing is true for like an else statement. So if I want to do something else, I can do that. Something else here. And now if I come back and refresh, there you go. Now, blade even includes a couple extra helpers. For example, in Ruby, you may be familiar with the fact that they offer an unless keyword. And that's basically equivalent to if not. But it sounds better, right?And that's basically equivalent to if not. But it sounds better, right? Unless this value is true, then I want to do that. Well, with blade, we can do that. Unless. And then down here, you would say end unless. And then in this case, we would say there are some people. And that's what we'll see in this case. OK, so let's do a quick recap. When you want to pass data to a view, you can send through an array as the second argument Recap and Facade Aside 5:17OK, so let's do a quick recap. When you want to pass data to a view, you can send through an array as the second argument to the view function. Now, it's possible you're learning from other sources as well. You're reading blog articles. So you also might see, just as an aside, you might see them do something like this. View make. In Laravel, this is what we call a facade. Now, you don't need to worry about this too much just yet. But just accept that these two are functionally equivalent.Now, you don't need to worry about this too much just yet. But just accept that these two are functionally equivalent. They're triggering the same bit of code behind the scenes. It's a global function. You're going to use it in virtually every controller or routes file in this example. So it makes sense to make it available like this. Anyways, just an aside. So you can pass an array as the second argument, or you can use this with method. With people equals people. And you could continue chaining that if you wanted to.With people equals people. And you could continue chaining that if you wanted to. Really, though, if you go beyond one, it gets kind of sloppy. So another example, of course, would be to use an array to the with method. With people is people. As I said before, any of these approaches are fine. If you want my advice, I think this kind of looks good. But it's up to you. There's no right or wrong. Finally, you learn just the basics, the initial introduction to using Blade.There's no right or wrong. Finally, you learn just the basics, the initial introduction to using Blade. And as you'll find, there's not that much to learn. You can take what you already know and quickly translate it over to Blade. And I think you'll see that stuff like this is far more enjoyable to write if we can make use of Blade. So anytime you want to echo out a value, yeah, no more stuff like this. You don't need to do that. Just do two braces. And that will, of course, translate to an echo statement.Just do two braces. And that will, of course, translate to an echo statement.