The task is the following: I want to transfer to all controllers the data about the site header, which I receive from the database. How can I do it?

I tried it like this, but it seems to be quite different:

public function handle($request, Closure $next) { $arr = ['a' => 'b', 'c' => 'd']; $request->request->set('first', $arr); return $next($request); } 

And then in the controller I try to address them like this:

 public function getIndex(Request $request) { dd($request->request->get('first')); } 

Who knows the thread how to do it right?

    2 answers 2

    Why transfer the site header data to the controllers? probably the challenge is ultimately to transfer to View.

     public function handle($request, Closure $next) { view()->share(['a' => 'b','c' => 'd']); // переменные $a и $c будут в view return $next($request); } 

      You need to register a singleton in the service container, the singleton will store data in its properties, and you can also get the data from the database

        $this->app->singleton('FooBar', function($app) { return new FooBar(); }); 

      Then get your object in the middleware constructor and save the data in it

        public function __construct(FooBar $foobar) { $this->foobar = $foobar; } public function handle($request, Closure $next) { $arr = ['a' => 'b', 'c' => 'd']; $this->foobar->set('first', $arr); return $next($request); } 

      Now the FooBar object will store data that can be used in controllers.

      • Is there really no special variable for this case? - Razzwan