Passing data into a view
In our web app, we will usually need to display some kind of data from our database or other data store. In Laravel, we can easily pass that data into our views.
Getting ready
For this recipe, we need to have completed the Creating and using a basic view recipe .
How to do it…
To complete this recipe, follow these steps:
Open the
routes.php
and replace our home and second routes to include the following data:Route::get('home', function() { $page_title = 'My Home Page Title'; return View::make('myviews.home')->with('title',$page_title); }); Route::get('second', function() { $view = View::make('myviews.second'); $view->my_name = 'John Doe'; $view->my_city = 'Austin'; return $view; });
In the
view/myviews
directory, openhome.php
and replace the code with the following code:<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Home Page : <?= $title ?></title> </head...