Laravel 9 | Displaying Data | Assign Default Value in Blade Template | Use Functions in Blade Template

Displaying Data

Core PHP Syntax Vs Blade Template Syntax

1. Core PHP Syntax:

<!--php
    echo $name;
?-->

2. Blade Template Syntax:

code path resources>views>home.blade.php

// It is use for echo/display the content
{{ $name }} 

/* It is use for decode html code 
 (It is use to execute html code)
 Above syntax render html code as it is */
{!! $name !!}

 Result is given below

This is basic code to pass values from routes>web.php

Route::get('/', function () {
    $content = "<h1>This is a main heading</h1>";
    $data = compact('content');
    return view('home')->with($data);
});

Assign Default Value in Blade Template

If expected value not found then assign default value as given

code path resources>views>home.blade.php

<h2>
    Welcome , {{ $name ?? "Guest" }}
</h2>

Basic route code is given below

code path routes>web.php

Route::get('/{name?}', function ($name=null) {
    $data = compact('name');
    return view('home')->with($data);
});

When value passed in url


When value is not passed in url it will show the default

Use Functions in Blade Template

Set route 

code path routes>web.php

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


Use functions syntax in blade template
code path resources>views>home.blade.php

<h2>
    Time , {{ time() }}
</h2>

<h2>
    Time , {{ date('d-m-y') }}
</h2>

Comments