Search code examples
laraveleloquent

How to dynamically manage the 'owners/{ownerUUID}' prefix in Laravel API routes?


In my Laravel application, I have many duplicate routes with the prefix owners/{ownerUUID}. For example, I have a route like this:

GET owners/{ownerUUID}/hotels/{hotelUUID}

This is an API call and I'm wondering if there's a way to pass the ownerUUID in a different manner instead of always including it in the GET route. I'd like to avoid duplicating this part of the route in many similar route definitions. What are the best practices to handle this situation in Laravel? The ownerUUID would be the session in which the user authenticates on the client side.


Solution

  • the way that you are using routes is totally fine.

    if you want to remove ownerUUID and hotelUUID from URL you have to use POST method to send variables to controller.

    for example :

    route:

    Route::post('/owners-hotels', [Controller::class, 'foo'])->name('owners-hotels');
    

    controller:

    public function foo(Request $request){
    
        $ownerUUID = $request->integer('ownerUUID');
        $hotelUUID = $request->integer('hotelUUID');
    
        // your code
    }
    

    then you can send post request to your api...