global variable for all controller and views

后端 未结 16 2112
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:22

    In Laravel 5.1 I needed a global variable populated with model data accessible in all views.

    I followed a similar approach to ollieread's answer and was able to use my variable ($notifications) in any view.

    My controller location: /app/Http/Controllers/Controller.php

    get_notifications();
            View::share('notifications', $notifications);
        }
    }
    

    My model location: /app/Models/Main.php

    namespace App\Models;
    
    use Illuminate\Database\Eloquent\Model;
    use DB;
    
    class Main extends Model
    {
        public function get_notifications() {...
    

提交回复
热议问题