global variable for all controller and views

how to make a global variable in laravel controller
laravel 5.7 global variable
laravel 5.8 global variable
laravel set global variable in middleware
laravel global variable for all views
laravel 6 global variable
laravel controller view
laravel 5.6 global variable

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

public function __construct() 
{
    // Fetch the Site Settings object
    $site_settings = Setting::all();
    View::share('site_settings', $site_settings);
}

Now i want to access $site_settings. in all other controllers and views so that i don't need to write the same code again and again, so anybody please tell me the solution or any other way so i can fetch the data from the table once and use it in all controllers and view.


At first, a config file is appropriate for this kind of things but you may also use another approach, which is as given below (Laravel - 4):

// You can keep this in your filters.php file
App::before(function($request) {
    App::singleton('site_settings', function(){
        return Setting::all();
    });

    // If you use this line of code then it'll be available in any view
    // as $site_settings but you may also use app('site_settings') as well
    View::share('site_settings', app('site_settings'));
});

To get the same data in any controller you may use:

$site_settings = app('site_settings');

There are many ways, just use one or another, which one you prefer but I'm using the Container.

global variable for all controller and views, global variable for all controller and views Now i want to access $site_settings. in all other controllers and views so that i don't need to write  Today, We want to share with you global variable for all controller and views in Laravel.In this post we will show you set global dynamic variables in Laravel 5.8, hear for Laravel define global constants Config php file we will give you demo and example for implement.In this post, we will learn about How to set a variable inside Laravel’s blade template with an example.


Okay, I'm going to completely ignore the ridiculous amount of over engineering and assumptions that the other answers are rife with, and go with the simple option.

If you're okay for there to be a single database call during each request, then the method is simple, alarmingly so:

class BaseController extends \Controller
{

    protected $site_settings;

    public function __construct() 
    {
        // Fetch the Site Settings object
        $this->site_settings = Setting::all();
        View::share('site_settings', $this->site_settings);
    }

}

Now providing that all of your controllers extend this BaseController, they can just do $this->site_settings.

If you wish to limit the amount of queries across multiple requests, you could use a caching solution as previously provided, but based on your question, the simple answer is a class property.

Set Global Variable accessible in all controller methods, Set Global Variable accessible in all controller methods. Posted 3 years ago by raziel79. Hi all, i have this route. Route::group(['middleware' => 'returnphase'],  Hello, Recently in one of my project we have an admin panel crated with Laravel. There was a requirement to setup global object that can be accessed in all controllers, views, models etc. Basically it was logged in user object with additional information so we can access details of user in anywhere. Here in this …


Use the Config class:

Config::set('site_settings', $site_settings);

Config::get('site_settings');

http://laravel.com/docs/4.2/configuration

Configuration values that are set at run-time are only set for the current request, and will not be carried over to subsequent requests.

global variable for all controller and views, global variable for all controller and views. In Laravel I have a table settings and i have fetched complete data from the table in the BaseController, as following When you're building a website featuring a whole lot of Views you most likely end up setting a lot of variables & properties in each and every ViewBag obj Find out the most widely-used patterns to store global ViewBag properties and have them available in every View's ViewBag object.


In Laravel, 5+ you can create a file in the config folder and create variables in that and use that across the app. For instance, I want to store some information based on the site. I create a file called siteVars.php, which looks like this

<?php
return [
    'supportEmail' => 'email@gmail.com',
    'adminEmail' => 'admin@sitename.com'
];

Now in the routes, controller, views you can access it using

Config::get('siteVars.supportEmail')

In the views if I this

{{ Config::get('siteVars.supportEmail') }}

It will give email@gmail.com

Hope this helps.

How to create a variable accessible to all controllers and views , My current setup right now is I instantiated it to a global variable in the start.php file then used it in the controllers to set values etc. then called it  Laravel 7.x and 6.x global variable in all views file Here, i will let you know how to set global variable in laravel 5 application. In this article i will set globle variable two way on simple view share and another using composer view share.


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

<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

use App\Models\Main as MainModel;
use View;

abstract class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    public function __construct() {
        $oMainM = new MainModel;
        $notifications = $oMainM->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() {...

Laravel Global Variable for All Controller and Views, Laravel Global Variable for All Controller and Views. Hello, Recently in one of my project we have an admin panel crated with Laravel. There  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.


Why you shouldn't set global variables in Base Controller, Everyone had a situation when one variable had to be accessible in every view file, for some of us it was $user, for others it was $currentPage  Global constant values throughout MVC site. Ask Question have a ViewBag.Phone set up for every view, EG my global variables in a different place or is there a


Define Global Variable in Laravel 5.8, you can define constants variable with string value, integer value, array value and you can access for all controller, all views, all blade files,  Global variables can be used for many of the same purposes as linked values (also referred to as shared values or linked dimensions). Global variables are much easier to find, change, and manage than linked values.


How to Define Global Variable in Laravel 5.8 Application, You can define constants variable with string value, integer value, array value and you can access for all controller, all views, all blade files, middleware too in