2

I try to determine a global variable in template blade:

global $result_view;

Below this I try call this:

 <?=$result_view?>

And get error: Undefined variable: result_view ()

hizbul25
  • 3,829
  • 4
  • 26
  • 39
Dev
  • 1,013
  • 7
  • 15
  • 37
  • 1
    Possible duplicate of [Laravel 5 - global Blade view variable available in all templates](http://stackoverflow.com/questions/29715813/laravel-5-global-blade-view-variable-available-in-all-templates) – Limon Monte Aug 13 '16 at 12:59
  • By global, do you mean that the variable should be available for all blade templates? – linuxartisan Aug 13 '16 at 14:13

2 Answers2

3

In Controller or Service provider , you can add

View::share('result_view', 'SomeValue');

in any Blade template in your project you can use,

{{ $result_view }}
Shìpu Ahamed
  • 430
  • 4
  • 16
  • 1
    Becareful when using this with wildcards like View::share('folder.*') especially when your variable is retrieving from a database query. Every blade file being included or rendered makes 1 extra query and ends up making tons of the same query per blade file. – Bill L. Jul 10 '20 at 07:36
  • very important note from @BillL. – Shìpu Ahamed Jul 11 '20 at 19:29
1

One way to do it is to define your variable in a config file and then you'll be able to get this var in all views, controllers etc.

{{ config('myConfig.someVariable') }}

Also, you can use view composer to set global variable.

Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279