How to use php variable in laravel blade?

Created At: 2020-10-02 16:00:00 Updated At: 2020-10-18 21:42:41

If you want to use a php variable in Laravel blade engine, you can do like this. Add this 

@php($x=0)

in your blade , where x is your variable. You can initialize as you want. You can use the variable on the blade, even use it as an increment or decrement.

If you want to use $x in your blade in foreach use like below

@foreach($var as $v)

  {{$x++}}

@endforeach

Of course you can also do 

{{$x--}}

You must not forget to use double curly braces.

Comment

Add Reviews