I am unable to understand why below is not working, I have defined my route like below
Route::apiResource('/api/support-staffs', SupportStaffController::class);
If I perform a route:list
I get
+--------+-----------+------------------------------------+------------------------+------------------------------------------------------------------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+-----------+------------------------------------+------------------------+------------------------------------------------------------------+------------+
| | GET|HEAD | api/support-staffs | support-staffs.index | Custom\SupportStaff\Controllers\SupportStaffController@index | auth:api
|
| | | | | | bindings
|
| | POST | api/support-staffs | support-staffs.store | Custom\SupportStaff\Controllers\SupportStaffController@store | auth:api
|
| | | | | | bindings
|
| | GET|HEAD | api/support-staffs/{support_staff} | support-staffs.show | Custom\SupportStaff\Controllers\SupportStaffController@show | auth:api
|
| | | | | | bindings
|
| | PUT|PATCH | api/support-staffs/{support_staff} | support-staffs.update | Custom\SupportStaff\Controllers\SupportStaffController@update | auth:api
|
| | | | | | bindings
|
| | DELETE | api/support-staffs/{support_staff} | support-staffs.destroy | Custom\SupportStaff\Controllers\SupportStaffController@destroy | auth:api
|
| | | | | | bindings
|
+--------+-----------+------------------------------------+------------------------+------------------------------------------------------------------+------------+
My supportStaff
happen to be users so I am trying to perform a dependency injection like below
public function show(User $user)
{
return $user;
}
And changing my route to
Route::middleware(['auth:api', 'bindings'])->group(function () {
Route::apiResource('/api/support-staffs', SupportStaffController::class)
->parameters(['support_staff' => 'user']);
});
Unfortunately I am receiving back an empty []
array. Why isn't support_staff
parameter being replaced with the user
parameter? I have confirmed by running route:list
that the parameter is still support_staff
My Answer:
The issue might be related to how you are defining the route and the controller method. When using dependency injection in Laravel, the parameter name in the controller method should match the route parameter name.
In your case, you are trying to inject a `User` model into the `show` method of the `SupportStaffController`. However, the route parameter is `{support_staff}` and not `{user}`. Therefore, Laravel is not able to resolve the dependency injection.
To fix this issue, you should update your controller method to match the route parameter name. Here's how you can do it:
php
public function show(User $support_staff)
{
return $support_staff;
}
By changing the parameter name in the `show` method to `$support_staff`, Laravel will be able to resolve the dependency injection correctly.
Additionally, make sure that the `User` model is imported at the top of your controller file:
php
use App\Models\User;
After making these changes, try accessing the `show` route with a valid `User` ID, and the dependency injection should work as expected.
Rate this post
4 of 5 based on 8888 votesComments