Reputation: 135
I understand that if I have a route of say api/users/{id} then this will get passed to the controller function as an $id param.
However if I have a api route of:
Route::patch('roadmap/{roadmapcourse}', 'RoadmapCourseController@update');
and a controller method:
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\RoadmapCourse $roadmapcourse
* @return \Illuminate\Http\Response
*/
public function update(Request $request, RoadmapCourse $roadmapcourse)
{
$data = $request->validate([
'user_id' => 'required|integer',
'course_id' => 'required|integer',
'stage' => 'required|integer',
'title' => 'required|string',
'creator' => 'required|string',
'url' => 'required|string',
'hours' => 'required',
'completed' => 'required|boolean'
]);
$roadmapcourse->update($data);
return response($roadmapcourse, 200);
}
Then I send a request to http://projectname/api/roadmap/2 - then 2 gets passed through as the param to the update function doesn't it?
But by the looks of the update function its expecting an instance of RoadmapCourse and not a single digit i.e. '2'?
Does Laravel, behind the scenes, search for a database entry for a RoadmapCourse with the id of 2, and then bring that in to the function as $roadmapcourse?
That's the only thing I can think of, and I can't find any documentation explaining what's going on.
P.S I also can't find any documentation regarding the variable naming convention of Class $variable i.e. RoadmapCourse $roadmapcourse, I understand what it does, just can't find any docs.
P.P.S I also can't find any documentation explaining the "docblocks" above a controller method e.g.
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\RoadmapCourse $roadmapcourse
* @return \Illuminate\Http\Response
*/
And what these '@param' declarations are actually for??
Any help or links to docs would be appreciated (I've been looking at the Laravel docs but can't find any mention or any of these things - hence posting on here)
Thanks!
Upvotes: 2
Views: 1963
Reputation: 14261
I understand that if I have a route of say api/users/{id} then this will get passed to the controller function as an $id param.
This is because the param will be stored a regular variable.
However if I have a api route of (...) and my controller looks like this:
public function update(Request $request, RoadmapCourse $roadmapcourse) { /** ... */ }
It doesn't work.
This is because you are trying to use Model Binding but, given your variable name (and route param) Laravel isn't able to resolve the proper class.
If you do this instead:
Route::patch('roadmap/{roadmapcourse}', 'RoadmapCourseController@update');
public function update(Request $request, $roadmapcourse)
{ // ^^^^^^^^^^^^^^
dd($roadmapcourse); // you'll get the value
}
Of course, you can tell laravel to properly resolve your path variable to get a record from your database. To do so, you'll need to config a Explicit Binding. From the docs:
Explicit Binding
To register an explicit binding, use the router's
model
method to specify the class for a given parameter. You should define your explicit model bindings in theboot
method of theRouteServiceProvider
class:public function boot() { parent::boot(); Route::model('user', App\User::class); }
Next, define a route that contains a {user} parameter:
Route::get('profile/{user}', function (App\User $user) { // });
So in your case, try this:
# app/Providers/RouteServiceProvider.php
public function boot()
{
parent::boot();
Route::model('roadmapcourse', App\RoadmapCourse::class);
}
Just then, Laravel will be able to resolve your route param:
Route::patch('roadmap/{roadmapcourse}', 'RoadmapCourseController@update');
Upvotes: 0
Reputation: 29288
Does Laravel, behind the scenes, search for a database entry for a
RoadmapCourse
with the id of2
, and then bring that in to the function as$roadmapcourse
?
Yes, indeed it does. See https://laravel.com/docs/5.8/routing#route-model-binding for more information.
Since the
$user
variable is type-hinted as theApp\User
Eloquent model and the variable name matches the{user}
URI segment, Laravel will automatically inject the model instance that has an ID matching the corresponding value from the request URI. If a matching model instance is not found in the database, a 404 HTTP response will automatically be generated.
As for the other questions (note, try to limit questions to a single, concise question in the future), the variable name doesn't matter; $roadmapcourse
could be anything, as long as the injected Model is correct. If you don't use RoadmapCourse
, it'll try to find whatever model you pass, based on the primary key (generally id
).
For the docblocks
, that's standard function commenting. @param
and @return
can be parsed and display those values in a REST API document, but by default, they don't do anything aside from provide a visual reference. You can safely ignore those if you want.
Upvotes: 1