Answer the question
In order to leave comments, you need to log in
How to replace the field on which the model is autoloaded in the resource method of the laravel controller?
Good day.
Standard resource controller
Model is loaded by id (api/image/{id})
public function show(Image $image) {}
How to replace id with another field?
PS If it's not difficult to remind you how it's called
Answer the question
In order to leave comments, you need to log in
This is the Route Model Binding .
If you would like model binding to use a database column other than id
when retrieving a given model class, you may override the getRouteKeyName
method on the Eloquent model:
/**
* Get the route key for the model.
*
* @return string
*/
public function getRouteKeyName()
{
return 'slug';
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question