0% found this document useful (0 votes)
11 views

Laravel Part-4

Uploaded by

waver58650
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Laravel Part-4

Uploaded by

waver58650
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Video-16 (Blade template in Laravel):-

------------------------------------------------------
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.

2. Blade's {{ }} echo statements are automatically sent through PHP's


htmlspecialchars function to prevent XSS attacks.
Example:-
{{$name}}
3. Calling Function :- You may also echo the results of any PHP function by
wrapping the function name in curly braces.
Example:-
{{ time() }}

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

3. If elseif else directive:-


@if(condition)
………………
@elseif(condition)
………………
@else

@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

2. Specific env directive


@ env(‘staging’)
//Running in env_name
@ endenv

3. Multiple specific env directive


@ env ([‘env1’ ,’env2’])
//Running in env1or env2
@ endenv

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

Break and Continue Directive : -


1. Break directive : -
@ foreach (array_name as value)
……………………..
@ if(condition)
@ break
@ endif
……………
@ endforeach

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

$loop->index The index of the current loop iteration (starts at


0).

$loop->iteration The current loop iteration (starts at 1).

$loop->remaining The iterations remain in the loop.

$loop->count The total number of items in the array being


iterated.

$loop->first Whether this is the first iteration through the


loop.

$loop->last Whether this is the last iteration through the


loop.

$loop->even Whether this is an even iteration through the


loop.

$loop->odd Whether this is an odd iteration through the


loop.

$loop->depth The nesting level of the current loop.

$loop->parent When in a nested loop, the parent's loop


variable.
Video-17 (Including subview inside view in Laravel):-
--------------------------------------------------------------------------
1. Include directive:-
Blade's @include directive allows you to include a Blade view from within
another view. All variables that are available to the parent view will be
made available to the included view:

Syntax:-
@ include (‘view_name)
@ include (‘view_name’, array)

Location of web:- routes\web.php


<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AboutController;

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

Location of view:- resources\views\welcome.blade.php


<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<title></title>
</head>
<body>
<h1> Welcome Page </h1>
<hr>
@include('contact')
</body>
</html>

Location of view : - resources\views\contact.blade.php


<h1> Contact Page</h1>
<ul>
<li>Noida </li>
<li>Patna </li>
<li>Delhi </li>
<li>Ranchi </li>
</ul>

2. Include directive with Controller : -


Location of Route : -resources\views\welcome.blade.php
<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Product;

Route::get('product',[Product::class,'show']);

Location of Controller : -App\Http\wController\Product


<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class Product extends Controller


{

function show()
{
$website='Clickcart';
return view('product',['web_name'=>$website]);
}

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>
<h1> {{$web_name}} : Product Page </h1>
@include('mobile_list')
</body>
</html>

Location of views : -resources\views\mobile_list.blade.php

<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>

4. includeWhen and includeUnless directive:-


If you would like to @include a view if a given boolean expression
evaluates to true or false, you may use the @includeWhen and
@includeUnless directives:
Syntax : -
@ includeWhen($boolean, ‘view_name’)
@ includeWhen($boolean, ‘view_name’, array)

@ includeUnless($boolean ,‘view_name’)
@ includeUnless($boolean ,‘view_name’ ,array)

Location of Controller : -App\Http\wController\Product


<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class Product extends Controller


{

function show()
{
$website='Clickcart';
$user= 'Admin';
$userType= $user=='Admin' ? true :false;
return view('product',['web_name'=>$website,
'utype'=>$userType]);
}

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>

@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’)

Location of Controller : -App\Http\wController\Product


<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class Product extends Controller


{

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]);
}

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>

//@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>

{{-- @includeWhen($utype, 'mobile_list') --}}


@once
<h1> It will be evaluted once per rendering cycle </h1>
@endonce

</body>
</html>

Video-18 (Raw PHP in Laravel):-


-------------------------------------------
You can use the Blade @php directive to execute a block of plain PHP within
your template:
Syntax:-
@ php
//PHP Code Here
@ endphp
Location of views : -resources\views\raw.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,
initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Row PHP</title>
</head>
<body>
<h3>Welcome page </h3>
@php
echo "Hello"
$name="Sanjeet Kumar Singh";
echo $name;
@endphp

</body>
</html>

Location of Route : -Route\web.php


<?php

use Illuminate\Support\Facades\Route;
Route::get('raw', function () {
return view('raw');
});

You might also like