Route Model Binding not working
Finally, after 2 days I found my answer and I would like to provide my answer here for everyone who maybe has the same problem.
For route binding to work, your type-hinted variable name must match the route placeholder name
For example my edit method
Here my route URI for edit
admin/file/{file}/edit
As you can see there is {file}
placeholder in the route definition, so the corresponding variable must be called $file
.
public function edit(Files $file)
{
return view('admin.edit',compact('file'));
}
I´ve stumbled across this again. I´m not sure if this was my own mistake or if this middlerware is missing by default.
Im using Laravel 8 and develop an API. I always got an empty array back as a response, when I tried to call a route that should implicitly bind to a model.
TL;DR
Check if the Kernel.php
file $routedMiddlewareGroups
api array has the SubstituteBindings::class
added to it. Otherwise your requests won´t get resolved correctly.
The request to the API | Get User by ID | Frontend, Vue.js
UserComponent.vue
methods: {
close() {
$('#user-modify-modal').modal('hide');
},
open() {
$('#user-modify-modal').modal({backdrop: 'static', keyboard: false});
this.fetchUserData(this.params.id);
},
async fetchUserData(id) {
await axios
.get('users/${this.params.id}')
.then((result) => {
this.user = result.data;
}).catch((err) => {
console.error(err);
});
}
},
Heres how I set up my API protected routes:
routes/api.php
//protected routes | API
Route::group(['middleware' => ['api', 'cors', 'json.response', 'auth:api'], 'prefix' => 'v1'], function() {
Route::post('/logout', [ApiLoginController::class, 'logout']);
Route::get('/user', function (Request $request) {
return $request->user();
});
Route::get('/users', [UserController::class, 'index']);
//the function im Testing...
Route::get('users/{user}', [UserController::class, 'show']);
Route::post('/users', [UserController::class, 'store']);
Route::put('/users/{user}', [UserController::class, 'update']);
Route::delete('/users/{user}', [UserController::class, 'show']);
});
UserController.php
...
/**
* Get user by ID
*
* @param Request $request
* @param User $user
* @return void
*/
public function show(User $user) {
return response()->json($user);
}
...
Without this middlerware SubstituteBinding::class
the Implicit Binding will NOT work!
Make sure to check your kernel / api settings
Kernel.php
/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
//\Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
'api' => [
'throttle:120,1',
EncryptCookies::class,
AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
];
Now when calling
http://localhost/api/v1/users/1
It returns the correct user data.
I know this problem already has a solution, but let me add some knowledge that might be useful to others...
As the documentation says, when using resource controllers, the resource routes will use parameters based on the "singularized" name of the resource. In the case of the question, since @siros was using the "file" resource name (singular form) in the route, the binding name in the controller method should be "file", although his model is named Files
. If he attempted to use:
Route::resource('admin/files','AdminController');
The controller would still need Files $file
to work, since file
is the singularized form of files
.
However, there is other (and more elegant) solution to the problem. You can change the type-hinted variable in the URL by providing a parameters
option in the configuration of the route, as shown in the documentation, which would automatically apply for the show
, edit
, update
and destroy
methods. This will let you keep the variable name in your controller matching the model name, for example.
So, in this case, @siros could use this in the routes file:
Route::resource('admin/file','AdminController', [
'parameters' => [
'file' => 'files'
]
]);
And he could use this in the controller:
public function edit(Files $files)
Hope this helps someone.