How to use laravel blade file - deesoft service

How to use laravel blade file

Deepak Tailor Image
Deepak Tailor - Jan 03 2023
How to use laravel blade file
Whats is blade file

Blade file is a view file in Laravel. Laravel uses the Blade template engine to display the view file on the browser. All these files are stored in the Views folder inside the Resources folder.

How to display data in blade file

Double brackets are used to print or display data in a blade file. like :

<p>{{ $first_name}}</p>
<p>{{ $last_name}}</p>
<p>{{ $roll_no}}</p>
<p>{{ $course}}</p>
<p>{{ $fees }}</p>
How to use if else statement in blade file.

We can also use if else statement in blade file. @if is used to start the if statement and @endif is used to close it.

// check status is active or inactive
@if($status == 'Active')
<h2>Your status is : {{ $status }} </h2>
@else
<h2>Your status is : {{ $status }} </h2>
@endif

Use multiple elseif statement

// check order status is [pending,dispatch,shipped,delivered,cancel,return]
@if($status == 'pending')
<h2>Your order status is :  Pending</h2>
@else if($status == 'dispatch')
<h2>Your order status is :  Dispatch</h2>
@else if($status == 'shipped')
<h2>Your order status is :  Shipped</h2>
@else if($status == 'delivered')
<h2>Your order status is :  Delivered</h2>
@else if($status == 'cancel')
<h2>Your order status is :  Cancel</h2>
@else if($status == 'return')
<h2>Your order status is :  Return</h2>
@endif

Nested if else in blade file

// use nested if else in blade file
@if($payment_method == 'COD')
    @if($order_status == 'pending')
        <p>your order is under process</p>
    @else
    <p>your order is complete</p>
    @endif
@else
    @if($payment_status == 'success')
        @if($order_status == 'pending')
            <p>your order is under process</p>
        @else
            <p>your order is complete</p>
        @endif
    @else
        <p>your order payment is pending</p>
    @endif
@endif
How to print data using foreach loop / for loop

We can use foreach / for loop to print data in blade file. Foreach starts the loop with @foreach and closes the loop with @endforeach and for loop starts the @for and close the loop with @@endfor.

// print data using foreach loop
@foreach($products as $single_product)
<p>Product Name : {{ $single_product->title }}</p>
<p>Product Quantity : {{ $single_product->Quantity }}</p>
<p>Product Price : {{ $single_product->Price }}</p>
@endforeach
// print data using foreach loop
@for($i = 0; $i < count($products); $i++)
<p>Product Name : {{ $products[$i]['title'] }}</p>
<p>Product Quantity : {{$products[$i]['quantity'] }}</p>
<p>Product Price : {{ $products[$i]['price'] }}</p>
@endfor
How to use custom php code in blade file

Laravel has also given us the facility to write custom php code. We start it with @php and close it with @endphp.

<p>{{ $name }}</p>
<p>{{ $profession }}</p>
<p>{{ $company }}</p>
<p>{{ $location }}</p>
How to print html content in blade file

We can also display custom html content in Laravel's blade file.

// print html content in blade file
@php
$html_data = '<div class="alert alert-success">this is success alert message.</div>';
@endphp

{!! $html_data !!}
Deepak Tailor Image
Deepak Tailor

My name is Deepak tailor as a fullstack developer. I have been in the IT industry (PHP, Nodejs, flutter) for the last 5 years. For professional and customize web development & app development, you can send inquiry on our email.
----
You can contact him at deepaktailor10@yahoo.in