Variable One-Liner
To define your one-liner variable in the Laravle Blade template, you can write your code like below. Do note that this variable is accessible only within this blade file unless it's imported to other blade files.
@php($firstName = 'John';) <h2>{{ $firstName }}</h2>
Laravel Multiple Variable
To define multiple variables in the Laravel Blade template, you can write it from within the @php directive body. This definition allows you to define a slightly more readable variable. Using this way you can also perform some minimal code logic to process the data.
@php $firstName = 'John'; $lastName = 'Doe'; $gender = 'Female'; $fullName = $firstName . ' ' . $lastName; @endphp
Laravel Blade Variable in Version 7
If you are using Laravel 7 then you can define the variable inline like below.
{{ $thisIsVariable = "You can define a variable using this method in Laravel 7" }}
Leave a reply