joshua jackson
joshua jackson

Reputation: 629

Laravel resource route parameters not overriding

I am building a Laravel 8 api. I'm trying to set up a show route for /api/access-type/{access_type}. I want to change the {access_type} to be {id}

This is the route definition:

Route::middleware(['auth:api'])->group(function () {

    /////////// not allowed to use these. Creating/deleting these resources would require code changes
    Route::apiResource(
        \App\Http\Controllers\AccessTypeController::$uri,
        'AccessTypeController'
    )->only(['index', 'show'])->parameters(['access_type' => 'id']);
});

When I look at the listed routes I can see that the route hasn't changed:

GET|HEAD | api/access-types/{access_type} | access-types.show | App\Http\Controllers\AccessTypeController@show | Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful | Illuminate\Routing\Middleware\ThrottleRequests:api | Illuminate\Routing\Middleware\SubstituteBindings | App\Http\Middleware\Authenticate:api

If I use postman to visit /api/access-types/3 I can see that there is no $request->id variable, but there is a $request->access_type variable, so the overwrite isn't working. What am I doing wrong? This should work according to: docs

Upvotes: 0

Views: 1020

Answers (2)

GianPierre Gálvez
GianPierre Gálvez

Reputation: 827

I fixed it on laravel version 10 with the following:

use App\Http\Controllers\OrderController;

Route::apiResource("orders", OrderController::class)
   ->parameters(['orders' => 'order_number'])
    ->only(['show','store','update']);

Upvotes: 0

joshua jackson
joshua jackson

Reputation: 629

I fixed it with the following:

Route::apiResource(
    \App\Http\Controllers\AccessTypeController::$uri,
    'AccessTypeController'
)->parameters(['access-types' => 'id']);

The parameter had to match the uri that I was setting it up for. I suppose this is so you can group multiple different resource routes into one.

Upvotes: 3

Related Questions