Views in Laravel
Views as the name suggests “It is the part of your website that is visible to the user”. Laravel provides a very efficient way for handling the HTML part of our website. Views are one of the powerful and underrated part of Laravel. Views can help you to maintain the structure and readability of code by separating each and every part of your HTML.
Views in Laravel have their separate place in the resources/views directory. All the views of your project is present in this directory. A simple view file looks something like this:
Laravel uses a templating engine for converting the view files into plane php file. The view files are stored with .blade extension before the php extension like this :
home.blade.php
Rendering View
For showing the view file or returning the view file from controller, use the view() global helper function like this:
public function index(){
return view('home');
}
Passing Data To View
There are multiple ways of passing data to the view file. Some of them are:
- Using compact method
public function index(){
$data = [
'name' => 'Dikshant',
'email' => 'dikshantraj2001@gmail.com',
'gender' => 'M'
];
'status' => 'active'; return view('home',compact('data','status'));}
- Passing data in an array as second parameter to view()
public function index(){
$data = [
'name' => 'Dikshant',
'email' => 'dikshantraj2001@gmail.com',
'gender' => 'M'
];
'status' => 'active'; return view('home',['data' => $data,'status' => $status]);}
- with() method
public function index(){
$data = [
'name' => 'Dikshant',
'email' => 'dikshantraj2001@gmail.com',
'gender' => 'M'
];
'status' => 'active'; return view('home')->with('data',$data)->with('status',$status);}
Useful Blade Directives
There are many blade directives that make our life quite easier. Some of them are:
- Echo data
To echo data in blade directly use this {{ }}
- Conditional statements
- Loops
There are multiple types of loops available in blade templating engine:
- Including and Extending
@include is used for including some other view in the file. You can even pass data directly as second parameter to this method.
@extends is used to extend a layout or any other file.
@yield is used for taking some data and place in other file. Generally used while extending.
@section is used to provide value to the variable mentioned with @yield.
@stack for stacking data one over the other . Generally used to dynamically add styles and scripts
- Auth and Guest directives
Used to check if the user is logged or not
- PHP
Sometimes there are situations where you want to use raw PHP in your code, you can easily do so by usng the @php directive
These are all the basic things one should know for easy and hurdle free development.
There are more advanced features also but all these can help you start your journey. Each and every thing is mentioned in the Laravel official documentation here:
If you like this blog, please hit that clap icon and if you want to read more such blogs on Laravel basics, do follow me and check my previous blogs here: