Laravel 5.8 – From The Ground Up - Laravel 5.8 Tutorial From Scratch – e04 – Passing Data to Views

Education, Programming

Laravel 5.8 – From The Ground Up

52 Lessons

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

Course Source Code

Hit us up on Twitter with any questions or comments @codertape (

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!

    add to database laravel admin laravel artisan Artisan Command artisan commend create artisan console artisan laravel command authentication in laravel belongsto belongsto laravel example coding tutorials create artisan command create command artisan create new command artisan Create your own crud laravel crud laravel tutorial crud resource laravel crud with file upload custom artisan commands custom artisan commands laravel custom laravel database configuration laravel database relationship digital ocean dotenv eager loading eager loading in laravel eager loading vs lazy loading laravel eloquent eloquent accessors eloquent mutators eloquent orm feature testing laravel fetching data in laravel filesystem laravel form requests full stack vue gate and policy laravel google optimize hasmany hasmany laravel hasmany laravel example hasmany relationship in laravel hasone hasone relationship in laravel How to create how to queue email how to use queue installing laravel intervention image laravel laravel 5 laravel 5 auth laravel 5.4 queue laravel 5.5 queue laravel 5.8 laravel 5.8 artisan command laravel 5.8 artisan console laravel 5.8 auth tutorial laravel 5.8 authentication laravel 5.8 commands laravel 5.8 crud laravel 5.8 crud example laravel 5.8 custom middleware laravel 5.8 deprecations laravel 5.8 eager loading laravel 5.8 elixir laravel 5.8 event listeners laravel 5.8 events laravel 5.8 feature laravel 5.8 features laravel 5.8 global middleware laravel 5.8 install laravel 5.8 lazy loading laravel 5.8 listeners laravel 5.8 middleware laravel 5.8 multi auth laravel 5.8 named resource routes laravel 5.8 named routes laravel 5.8 new feature laravel 5.8 own artisan command laravel 5.8 queue laravel 5.8 queues laravel 5.8 routing laravel 5.8 telescope laravel 5.8 tutorial laravel 5.8 user auth laravel 5.8 user authentication laravel 5.8 what's new laravel 5.9 laravel admin permissions laravel api laravel artisan command laravel artisan console laravel assets laravel auth laravel auth role laravel authentication laravel authorization laravel axios post example laravel background process laravel background task laravel background worker laravel beginner to master laravel belongsto laravel belongsto vs hasone laravel belongstomany laravel best packages laravel best practices laravel best tutorial laravel bootstrap laravel command laravel command line laravel commands tutorial laravel composer install laravel connect db laravel console command laravel console testing laravel contact form send email laravel controllers laravel create laravel crud laravel crud policy laravel custom command laravel custom middleware laravel database laravel database configuration laravel database relationships laravel database seeder laravel database settings laravel db seed laravel db settings laravel debugging laravel deploy aws laravel deploy digital ocean laravel deploy on digitalocean laravel deploy on server laravel deploy to production laravel deployment laravel digitalocean laravel drag and drop file upload laravel dropzone file upload laravel dropzone image upload laravel e-commerce laravel eager loading laravel eager loading tutorial laravel eager loading with condition laravel elixir laravel eloquent laravel eloquent belongsto laravel eloquent crud laravel eloquent hasmany laravel eloquent where laravel eloquest tutorial laravel events and queue laravel events tutorial laravel factory laravel feature test laravel feature testing laravel fetching data from database laravel file storage laravel fillable example laravel flash message notification laravel for beginners laravel form validation laravel forms laravel forms bootstrap snippets laravel from scratch laravel from the ground up laravel front end laravel frontend tutorial laravel gate and policy tutorial laravel global middleware laravel hasmany laravel hasone laravel i18n laravel installation laravel installation mac laravel intervention image tutorial laravel javascript tutorial laravel jobs queue laravel language laravel language switcher laravel language tutorial laravel languages laravel lazy loading laravel listener event laravel listeners laravel localization laravel login laravel mailable tutorial laravel many to many relationship example laravel markdown email laravel markdown mail laravel mass assignment laravel middleware laravel migrate fresh laravel mix laravel mix 5.8 laravel mix vue laravel model factory tutorial laravel multiple language laravel mysql json laravel named resource routes laravel named routes laravel new features laravel news laravel nginx laravel node modules laravel npm install laravel npm run watch error laravel one to one laravel paginate laravel paginate link laravel pagination laravel pagination 5.8 laravel pagination links laravel pagination tutorial laravel phpunit laravel phpunit testing laravel pivot table example laravel policy laravel preview laravel query optimization laravel queue laravel queue event listener laravel react laravel register user laravel registration laravel registration validation laravel relationships laravel reset password laravel role middleware laravel role permission laravel roles laravel route group middleware laravel routes laravel routing laravel routing with parameters laravel sass laravel save file to database laravel scopes laravel seo tutorial laravel server laravel session data laravel ssl laravel supervisor laravel tailwind css laravel tailwind setup laravel tdd laravel tdd tutorial laravel telescope laravel telescope installation laravel telescope tutorial laravel test driven development laravel test workflow laravel testing laravel testing controllers laravel testing tutorial laravel tools laravel translation laravel tutorial laravel tutorial for beginners laravel ubuntu server laravel ubuntu tutorial laravel unit testing controllers laravel upload file laravel upload image to storage laravel upload multiple files at once laravel upload multiple image to database laravel upload multiple images laravel url slug laravel vue laravel vue js crud laravel vue setup laravel vue tutorial laravel vue.js laravel webpack error laravel webpack tutorial lazy loading lazy loading laravel lazy loading vs eager loading learn laravel learn laravel framework step by step localization in laravel localization laravel many to many laravel mvc tutorial for beginners in php mvc tutorial laravel mysql mysql relational database mysql relationship n + 1 problem n + 1 problem laravel new artisan command npm install npm run dev npm run watch laravel one to many one to one one to one laravel own artisan command pagination pagination bootstrap pagination bootstrap php mysql pagination laravel pagination laravel bootstrap pagination links pagination php php php artisan php carbon immutable php framework php framework 2019 php what's new 2019 phpunit phpunit laravel phpunit testing pivot laravel policy laravel polymorphic relationships queue and events queue email queue for laravel queue jobs queue laravel 5.8 registration form relationship laravel eloquent relationship mysql restful controller role permission in laravel roles laravel route model binding laravel routing in laravel save image to database seo friendly seo friendly content writing seo friendly website seo laravel seo optimization simple pagination laravel slugify sqlite relational database sqlite relationships between tables symfony command console symfony laravel tailwind css tailwind laravel mix telescope laravel tutorial laravel unit testing unit testing laravel upload file laravel upload file php upload image laravel upload image to database php upload images laravel upload multiple files in php url slug laravel url slug php vue vue.js vuejs laravel why use laravel queue why use queue