简体   繁体   中英

laravel share variable in all views as reference

I really want a very simple thing

1) I Share a global variable from AppServiceProvider like below

public function boot() {
    view()->share('css', new CSS());
}

2) I set some property values it In a View (index.blade.php) like blow

$css->publicProperty = "Something"; // changed in index.blade.php

3) In another View (contact.blade.php) i check the $css->publicProperty variable but it doesn't give me the change value

dd($css->publicProperty) // give me the initial value in contact.blade.php

Now i need a way that share a variable as global also as reference .

So that i change in one view and it will retain the change in other views also.

You should use session on your controller.

Set session like this:

Session::put('name','css');

And you can change that in view like this:

 {{Session::put('name','css')}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM