DEV Community

Bijay Kumar Pun
Bijay Kumar Pun

Posted on

1

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');

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay