Laravel Part-4
Laravel Part-4
------------------------------------------------------
1. Blade is the simple, yet powerful templating engine that is included with
Laravel. Unlike some PHP templating engines, Blade does not restrict you
from using plain PHP code in your templates.
2. Blade template files use the .blade.php file extension and are typically
stored in the resources/views directory.
Display Data :-
1. You may display data that is passed to your Blade views by wrapping the
variable in curly braces.
Note:-
You can put any PHP code you wish inside of a Blade echo statement.
Comment :-
Blade also allows you to define comments in your views. However, unlike HTML
comments, Blade comments are not included in the HTML returned by your
application:
{{ -- This comment will not be present in the rendered HTML -- }}
Conditional Directive:-
1. If directive :-
@if(condition)
………………
@endif
2. If else directive:-
@if(condition)
………………
@else
………………
@endif
@endif
4. Unless directive : -
@ unless(condition)
………………..
@ endunless
5. isset directive :-
@ isset (var)
…………….
@ endisset
6. Empty directive: -
@ empty (var)
……………
@ endempty
Authentication Directive : -
1. Auth directive : -
@ auth
// User is authenticated
@ endauth
2. Guest directive : -
@ guest
// User is not authenticated
@ endguest
3. Auth directive : -
@ auth (‘admin’)
// User is authenticated
@ endauth
4. Guest directive : -
@ guest (‘admin’)
// User is not authenticated
@ endguest
Environment Directive : -
1. Production directive
@ production
//production specific content
@ endproduction
Switch Directive : -
1. Switch directive
@ switch(expression)
@ case (expression 1)
……………………
@ break
@ case (expression 2)
……………………
@ break
@ default
……………..
@ endswitch
Loop Directive : -
1. for directive :-
@ for(initial; condition; increment/Decrement)
……………………….
@ endfor
2. foreach directive :-
@ foreach(array_name as value)
……………………….
@ endforeach
3. forelse directive:-
@ forelse (array_name as value)
…………………….
@ empty
…………..
@ endforelse
4. While directive :-
@ while(condition)
……………….
@ endwhile
2. Continue directive : -
@ foreach (array_name as value)
……………………..
@ if(condition)
@ continue
@ endif
……………
@ endforeach
3. Break directive : -
@ foreach (array_name as value)
……………………..
@ break(condition)
……………
@ endforeach
4. Continue directive : -
@ foreach (array_name as value)
……………………..
@continue(condition)
……………
@ endforeach
Loop Variable : -
1. A $loop variable will be available inside of your loop. This variable
provides access to some useful bits of information such as the current
loop index and whether this is the first or last iteration through the loop.
Example:-
@ foreach($users as $user)
@ if($loop->first)
This is the first iteration.
@ endif
@ endforeach
2. If you are in a nested loop, you may access the parent loop's $loop
variable via the parent property:
Example:-
@ foreach($users as $user)
@ foreach($user->posts as $post)
@ if($loop -> parent-> first)
…………………
@ endif
@ foreach
@ endforeach
Property Description
Syntax:-
@ include (‘view_name)
@ include (‘view_name’, array)
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AboutController;
Route::get('/', function () {
return view('welcome');
});
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Product;
Route::get('product',[Product::class,'show']);
namespace App\Http\Controllers;
use Illuminate\Http\Request;
function show()
{
$website='Clickcart';
return view('product',['web_name'=>$website]);
}
<ul>
<li>SAMSUNG</li>
<li>I-phoe</li>
<li>Moto</li>
<li>Redmi</li>
<li>POCO</li>
</ul>
3. includeIf directive:-
If you would like to @include a view that may or may not be parent, you
should use the @ includeIf directive.
Syntax : -
@ includeIf(‘view_name)
@ includeIf(‘view_name’ ,array)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<title>Product Page</title>
</head>
<body>
<h1> Product Name </h1>
<hr>
<h1> {{$web_name}} : Product Page </h1>
@includeIf('mobile_list1')//If exist then show the data otherwise
don’t give any error
</body>
</html>
@ includeUnless($boolean ,‘view_name’)
@ includeUnless($boolean ,‘view_name’ ,array)
namespace App\Http\Controllers;
use Illuminate\Http\Request;
function show()
{
$website='Clickcart';
$user= 'Admin';
$userType= $user=='Admin' ? true :false;
return view('product',['web_name'=>$website,
'utype'=>$userType]);
}
@includeWhen($utype, 'mobile_list')
@includeUnless($utype, 'mobile_list')
</body>
</html>
5. each directive:-
1. You may combine loops and includes into one line with Blade's
@each directive:
Syntax:-
@ each( ‘view_name’ , array, item)
2. you may also pass a fourth argument to the @each directive. This
argument determines the view that will be rendered if the given array
is empty.
Syntax:-
@ each( ‘view_name’ , array, item , ‘view_name’)
namespace App\Http\Controllers;
use Illuminate\Http\Request;
function show()
{
$website='Clickcart';
$user= 'Admin';
$userType= $user=='Admin' ? true :false;
$students=[‘sanjeet’ , ‘Rohan’ ,’Sohan’];
return view('product',['web_name'=>$website,
'utype'=>$userType,’students’=>$students]);
}
//@includeWhen($utype, 'mobile_list')
//@includeUnless($utype, 'mobile_list')
@ foreach($students as $stu)
<h4> {{$stu}} </h4>
@ endforeach
// OR
@ each (‘students’, $students,’stu’)
</body>
</html>
6. once directive:-
The @once directive allows you to define a portion of the template that
will only be evaluated once per rendering cycle.
Syntax : -
@ once
…………..
@ endonce
Location of views : -resources\views\product.blade.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<title>Product Page</title>
</head>
<body>
<h1> Product Name </h1>
<hr>
</body>
</html>
</body>
</html>
use Illuminate\Support\Facades\Route;
Route::get('raw', function () {
return view('raw');
});