Laravel 5.8 – From The Ground Up
Lección 4 de 52 · Laravel 5.8 Tutorial From Scratch – e04 – Passing Data to Views · Educación
Laravel 5.8 Tutorial From Scratch – e04 – Passing Data to Views
so now we're gonna be talking about passing data into a view a lot of times your view is gonna need a lot of data so how do we pass data into the view so if
you recall at the end of the previous lesson we refactor all of our routes into this single line notation this single line notation is great for cleanliness but it's not so great when
you need to pass in some data so we're gonna actually have to revert to be able to pass in some data so we'll say route and we'll create a new one and you get route and we're gonna say customers it's
gonna be a new route that doesn't exist here and we're gonna say function and inside of our closure if you remember we can return a view and the view that we're gonna return is a new view that
doesn't exist yet call customers so this new view customers I actually want to put that inside subdirectories typically you won't have all of your views under a single directory that's gonna get
cluttered really quick so I'm going to add a new file here and I'm gonna call it may be something like internals slash customers that blade dot PHP so now we see that PHP storm has created a
directory called internals inside views and we're going to have this customers that blade that PHP so let's have an h1 we'll say customers and of course I want a list here of customers but this list
of course needs to be dynamic and this data needs to come from something else so let's go back to Chrome and let's visit slash customers and everything blows up it says the views customers is
not found that makes sense because customers is not sitting in the views directory customers is sitting inside internals so how do we tell laravel to look for a view inside a directory well
we do that using the dot syntax so you can say internals dot customers and so now it's going to look inside the internals directory and then find the customer stop blade dot PHP file let's
go back to Chrome hit refresh there we go so now we are working now I do prefer the dot syntax however you can always use the slash notation if you wanted to we refresh we get the exact
same thing but I do think the dot notation looks very clean so I would probably urge you to stick to the dot notation so with that figured out let's create a dataset
we'll say customers equals and it's gonna be an array of customers and we're gonna have John Doe let's have Jane Doe and finally let's have Bob the Builder fair enough
so we have this customers variable that has three customers inside how do I pass this into my view well any view as a second argument can actually receive an array so in this array we're gonna say
customers that's gonna be my key and we're gonna match that up to the variable customers and just like that you're gonna have this customers variable available to you in your view
so how do we access it open a PHP tag and inside here we'll say for each customers ask customer we're gonna want to echo go SEC Ally customer and then close our Li tag alright let's check out
the results in the browser and there we are so we now have our data being passed into the view just to show you that one more time we have this array here John Doe Jane Doe and Bob the Builder and as
a second argument to our view we're passing it as customers now this key here is what we are using here so if we change that to another name then we would have to use for each and then the
variable names will no longer be customers it will be another name and if we go back to our browser and refresh we get the exact same thing so just know that this key here is what is going to
be referenced to over here it doesn't matter what you call your variable here what matters is what you kid when you're passing in this array here now of course you can only pass
in one array into the view and that makes sense so you're gonna have a big array of data and each of the keys will become your variable names in your view so to wrap that up I do want to clean up
this view a little bit this is kind of nasty right now right so this is where blade comes in and blade does a great job of simplifying all of this for you so any blade specific syntax will always
start with the @ symbol and then we'll say for each very similar to what you're going to do in PHP so for each customers ass customer but then you don't have to do anything else you can actually
continue to write HTML from this point so we'll say Li and then to get this customer variable in here we're going to use two curly brackets and in the middle of those curly brackets we're going to
put customer and then to finish this for each we're gonna say at symbol again end for each so much cleaner syntax right so this will do the exact same thing and to prove it I'm gonna hit refresh again and
we get an error and of course it's looking for customers remember I had changed it to another name so let's change this back to customers and there we are
we are back to having our list of customers to wrap it all up we have three customers and we're passing that in with the key customers and then in here we're using this new blade syntax
for each customer's being the variable name as a customer and we're simply echoing it out inside our unordered list you're making great progress keep toying around with this idea until you grasp
the concept of passing data to a view and when you're ready let's move on to the next lesson
Every view will likely need some data passed to it. As we continue in our journey, let’s handle a very simple customers list using some dynamic data.
For the best experience, follow along in our interactive school at https://www.coderstape.com
Resources
Course Source Code
https://github.com/coderstape/laravel-58-from-scratch
Hit us up on Twitter with any questions or comments @codertape (https://twitter.com/CodersTape)
About This Course
Ready to get started on your path to Laravel Artisan? In this series, we are breaking down all of the basics of Laravel to get you comfortable using the world’s most popular PHP framework. Let’s get started!