Search code examples
phplaravelrestlaravel-10

Controller unable to access model ID despite correct setup. I am using Laravel 10 for this REST API


When I tried to access the ID of a model (JenisSurvey) within the show method of the JSController, the ID is always null, even though the model is correctly retrieved. Interestingly, I have other controllers in my application where similar operations work perfectly fine. For example, if I use the User model in a different controller, I can access the ID without any issues.

I've already checked the route configuration, model binding, auto-loaded files, controller logic, and the data with the searched ID exists in the database. Everything seems to be in order, and the logic mirrors that of other controllers where similar operations work.

Here's a simplified version of the show method in the JSController:

public function show(JenisSurvey $jenisSurvey)
{
    dd($jenisSurvey->id); // This always returns null

    if (!$jenisSurvey) {
        return response()->json(['error' => 'Data not found.'], 404);
    }
    return new JSResource($jenisSurvey);
}

Interestingly, I have other controllers in my application where similar operations work perfectly fine. For example, if I use the User model in a different controller (UserController), I can access the ID without any issues. Here's a simplified version of the show method in the UserController for comparison:

public function show(User $user)
{
    dd($user->id); // This returns the correct ID
    return new UserResource($user);
}

Additionally, when I switch the model and resource in the UserController to JenisSurvey, it works as expected.

// UserController but with jenis survey model and resource
public function show(JenisSurvey $user)
{
    dd($user->id); // This returns the correct ID
    return new JSResource($user);
}

Here are the routes:

Route::group(['namespace' => 'App\Http\Controllers', 'middleware' => 'auth:sanctum'],function () {
    Route::apiResource('users', UserController::class)->middleware(['auth','verified','role:admin']);
    Route::apiResource('js', JSController::class)->middleware(['auth','verified','role:admin|nasabah']);
});

Here are the screenshots below.

JSController(Jenis Survey Controller):

enter image description here

UserController using user's model and resource:

enter image description here

UserController using JenisSurvey's model and resource:

enter image description here

JSController (var dumping the whole model):

photo

UserController (with Jenis Survey model and resource):

photo

Given that the issue seems to be specific to the JSController (Jenis Survey Controller) file, I suspect there might be something unique about its setup or environment. Any insights or suggestions on how to troubleshoot this issue further would be greatly appreciated.


Solution

  • You should not assume the resource name will be jenisSurvey when all you have provided to the resource route builder is js.

    By doing a quick test L10:

    Route::apiResource('js', TestController::class);
    

    php artisan route:list --path=api/js shows:

    GET|HEAD    api/js .................... js.index
    POST        api/js .................... js.store
    GET|HEAD    api/js/{j} ................ js.show
    PUT|PATCH   api/js/{j} ................ js.update
    DELETE      api/js/{j} ................ js.destroy
    

    so your resource in your controllers would be referred as $j

    If you wish to keep the same route structure, you can overwrite the auto generated name on the route declaration

    Route::apiResource('js', TestController::class)->parameter('js', 'jenisSurvey');