global variable for all controller and views

后端 未结 16 2140
Happy的楠姐
Happy的楠姐 2020-11-27 15:54

In Laravel I have a table settings and i have fetched complete data from the table in the BaseController, as following

public function __construct() 
{
             


        
16条回答
  •  悲&欢浪女
    2020-11-27 16:13

    I have found a better way which works on Laravel 5.5 and makes variables accessible by views. And you can retrieve data from the database, do your logic by importing your Model just as you would in your controller.

    The "*" means you are referencing all views, if you research more you can choose views to affect.

    add in your app/Providers/AppServiceProvider.php

    composer('*', function(View $view) {
                $site_settings = Setting::all();
                $view->with('site_settings', $site_settings);
            });
        }
    
        /**
         * Register any application services.
         *
         * @return void
         */
        public function register()
        {
    
        }
    }
    

提交回复
热议问题