0

I am working with Laravel and in the controller I am using a middleware function inside constructor as following

  class BaseController extends Controller
   {
    //
        public function __construct(){        

    $this->middleware(function ($request, $next) {

        $selected_country_id = session()->get('browse_country_id')??4;
       
        return $next($request);
    });
    // end of middleware function

 //back to constructor function 
// How i can use selected_country_id here  for example 
dd($selected_country_id);
}
 }

my question is how to use the variable $selected_country_id outside the middleware function

Mokhtar Ghaleb
  • 423
  • 13
  • 26

1 Answers1

0

Flash it into the request with

$request->selected_country_id = $selected_country_id;
// or
$request->merge(["selected_country_id" => $selected_country_id]);
// or
$request->attributes->add(['selected_country_id' => $selected_country_id]);

And retrieve the value with

$request->selected_country_id;
badfrog
  • 16
  • 1
  • 3