how to define a variable in laravel blade

13,109

Solution 1

I agree with @Kiril Ivanov answer, but if you still want to do that you can use

@php ($variable = 'test')

Thanks

Solution 2

no, there is no way to define a variable with blade syntax except using the php syntax you have pointed. actually it is not a good practice to define variables in your views and do complex stuff except loops and conditional statements

Share:
13,109

Related videos on Youtube

Amirmasoud
Author by

Amirmasoud

Updated on June 04, 2022

Comments

  • Amirmasoud
    Amirmasoud almost 2 years

    How can define a variable in view side(blade) in laravel? I found that I can do it in this way:

     <?php $var = 'something' ?>
    

    but is there any way to do this like {{ $var = 'something' }} or @var1 = 'something' ?(ofcourse without print it)

    • c00p3r.web
      c00p3r.web over 7 years
      In this case you're just using Blade in a different way than it was designed for... You shouldn't put your data logic here... Just representation logic of already processed data...
  • Ja22
    Ja22 over 6 years
    Thank you. This was helpful