A
A
Alexey Markov2018-11-21 09:57:58
Laravel
Alexey Markov, 2018-11-21 09:57:58

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

1 answer(s)
A
Alexey Ukolov, 2018-11-21
@orbitroom

This is the Route Model Binding .
If you would like model binding to use a database column other than idwhen retrieving a given model class, you may override the getRouteKeyNamemethod 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 question

Ask a Question

731 491 924 answers to any question