DEV Community

Bijay Kumar Pun
Bijay Kumar Pun

Posted on

Inflating views and passing variable in Laravel

Excerpts from the book Laravel Up and Running by Matt Stauffer

View Formats
There are two formats of views in Laravel right out of the box:
example.blade.php
example.php

The first one is rendered by the Blade engine, while the latter is rendered by the PHP engine. They both reside in resources/views/.

View Loading Mechanism
Three ways to load view;
-view()
-View::make()
-Injecting with Illuminate\View\ViewFactory

Simple View Usage:

Route::get('/,function(){
return view('home');
});

Passing variable to view
Route::get('work',function(){
return view('tasks.index')
->with('tasks',Task::all());
});

The above closure loads the resources/views/tasks/index.blade.php or resources/views/tasks/index.php view and passes a single variable name tasks with the result of the result of the Task::all() eloquent method.

Using View Composers to share variables with every view
view()->share('view_variable','view_value');

Top comments (0)