How to Set Variables in a Laravel Blade Template
It is discouraged to do in a view so there is no blade tag for it.
If you do want to do this in your blade view, you can either just open a php tag as you wrote it or register a new blade tag. Just an example:
<?php
/**
* <code>
* {? $old_section = "whatever" ?}
* </code>
*/
Blade::extend(function($value) {
return preg_replace('/\{\?(.+)\?\}/', '<?php ${1} ?>', $value);
});
assign a value to a variable in blade file
@if($user_role=='2')
@php $status='1'; @endphp
@endif
@if($user_role=='3')
@php $status='2'; @endphp
@endif
@if($user_role=='4')
@php $status='3'; @endphp
@endif
you can check the value by adding echo
@if($user_role=='4')
@php echo $status='3'; @endphp
@endif
laravel blade template variables
Assume due_at
is a timestamp.
@foreach ($farmer->tasks as $task)
@if (Carbon::parse($task->pivot->due_at) < Carbon::now())
<?php $style = 'alert alert-danger'; ?>
@elseif (Carbon::parse($task->pivot->due_at) > Carbon::now())
<?php $style = 'alert alert-success'; ?>
@else
<?php $style = ''; ?>
@endif
<div class="list-group-item {{ $style }}">{{$task->name}} <span class="glyphicon glyphicon-calendar"> {{ $task->pivot->due_at }}</span> <span class="glyphicon glyphicon-pencil"></span><span class="glyphicon glyphicon-trash"></span></div>
@endforeach
declaring simple variables in views in Laravel
The Blade {{ }}
will echo out what you are doing.
You should do it like this:
<?php $a = 0; ?>
@foreach($fans as $fan)
@foreach ($array as $x)
@if ($fan->fbid == $x)
<?php $a++; ?>
@endif
@endforeach
@endforeach
{{$a}}
how to define a variable in laravel blade
I agree with @Kiril Ivanov answer, but if you still want to do that you can use
@php ($variable = 'test')
Thanks
How to Set Variables in a Laravel Blade Template
It is discouraged to do in a view so there is no blade tag for it.
If you do want to do this in your blade view, you can either just open a php tag as you wrote it or register a new blade tag. Just an example:
<?php
/**
* <code>
* {? $old_section = "whatever" ?}
* </code>
*/
Blade::extend(function($value) {
return preg_replace('/\{\?(.+)\?\}/', '<?php ${1} ?>', $value);
});
How to set a variable in blade (laravel 5.3)?
You are using laravel-4 template comment syntax to define/set variables which is may be not working with L5.x.
But you can try @php ($p3 = $key['p3'])
OR
@php
$p3 = $key['p3']
@endphp
Above both are same.
Further you go with create own service provider like
1 create BladeServiceProvider
:
<?php
//app/Providers/BladeServiceProvider.php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class BladeServiceProvider extends ServiceProvider
{
public function boot()
{
/* @datetime($var) */
\Blade::extend(function($view, $compiler)
{
$pattern = $compiler->createOpenMatcher('datetime');
return preg_replace($pattern, '$1<?php echo $2->format(\'m/d/Y H:i\')); ?>', $view);
});
/* @eval($var++) */
\Blade::extend(function($view)
{
return preg_replace('/\@eval\((.+)\)/', '<?php ${1}; ?>', $view);
});
}
public function register()
{
//
}
}
2 Register BladeServiceProvider
:
<?php
//in config/app.php add
return [
// ...
'providers' => [
// ...
'App\Providers\BladeServiceProvider',
Clear complied artisan clear-compiled
Assign value to variable @datetime($updated_at)
OR
@eval($var = 1)
Taken reference from: Laravel 5 alternative
Related Topics
How to Set the Default Value of an Attribute on a Laravel Model
Using Maatwebsite Excel 3.1 Export How to Set Background Color for Cells and Font Size for Heading
How to Stop My PHP Form Resubmitting Input After Page Refresh
Showing Image Binary Data Using JavaScript
How to Resolve MySQL Port 3306 Error on Wamp
Combining 'Where' and 'Like' Statements by Using the Ci Activerecords
How to Update Multiple Select Box Values in PHP MySQL
Assign Jquery Value to PHP Variable Inside Ajax Sussess
How to Sends Two Values in Select Dropdown Using PHP
Open Modal from Another Page Laravel
Auto Update Cart on Quantity Change in Woocommerce Cart Page
Send File Via Curl from Form Post in PHP
404 Not Found When Passing Variable to Route in Laravel
Retrieve MySQL Value from PHP for Radio Buttons
Remove Useless Zero Digits from Decimals in PHP
404 Not Found; Apache Server Can't Load Simple PHP File