Home > OS >  Laravel accessing route parameter in service provider
Laravel accessing route parameter in service provider

Time:08-25

I have a route in laravel

Route::get('{company}/dashboard', [DashboardController::class, 'company'])->name('dashboard');

and I got a new Service-Provider. In that Service-Provider I want to access the variable company from the route.

I can access the request with

request()
// OR
$request = app(\Illuminate\Http\Request::class);

but the Parameter is not present. The request dump look like this:

^ Illuminate\Http\Request {#42 ▼
   attributes: Symfony\Component\HttpFoundation\ParameterBag {#44 ▼
    #parameters: []
  }
   request: Symfony\Component\HttpFoundation\InputBag {#43 ▶}
   query: Symfony\Component\HttpFoundation\InputBag {#50 ▶}
   server: Symfony\Component\HttpFoundation\ServerBag {#46 ▶}
   files: Symfony\Component\HttpFoundation\FileBag {#47 ▶}
   cookies: Symfony\Component\HttpFoundation\InputBag {#45 ▶}
   headers: Symfony\Component\HttpFoundation\HeaderBag {#48 ▶}
  #content: null
  #languages: null
  #charsets: null
  #encodings: null
  #acceptableContentTypes: null
  #pathInfo: "/test/dashboard"
  #requestUri: "/test/dashboard"
  #baseUrl: ""
  #basePath: null
  #method: null
  #format: null
  #session: null
  #locale: null
  #defaultLocale: "en"
  -preferredFormat: null
  -isHostValid: true
  -isForwardedValid: true
  #json: null
  #convertedFiles: null
  #userResolver: null
  #routeResolver: null
  basePath: ""
  method: "GET"
  format: "html"
}

any idea how I can access the parameter company?

Greetings C14r

CodePudding user response:

You can access the company parameter directly on the $request:

$company = $request->company;

CodePudding user response:

You can able to access the route param using the Request class

$company  = $request->route('company');
  • Related