If you wish to use your own resolution logic, you may use the Route::bind method. The Closure you pass to the bind method will receive the value of the URI segment and should return the instance of the class that should be injected into the route:
public function boot()
{
parent::boot();
Route::bind('user', function ($value) {
return Post::where('id', $value)
->orWhere('slug', $value)
->first();
});
}
@bashy True, I didn't notice he wanted to use one OR the other. The solution found before seems to be the best. Matching both at the same time just seems to be an unnecessarily complicated thing to do, unless you would want to compare them for safety reasons.
I have a similar problem but mine is slightly more complicated because my slugs are numeric as well so if I do orwhere and first() and my slug is 1000 and I have a record with id 1000 as well it will just find one of them. Any idea how could I solve this? The only thing that occurs to me is have different routes with different methods but I would like to have the same route and method if possible.
Another alternative solution is to use the resolveRouteBinding method on your model to customize the resolution logic. In my case I search for numeric IDs and then for slugs:
/**
* Retrieve the model for a bound value.
*
* @param mixed $value
* @param string|null $field
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function resolveRouteBinding($value, $field = null)
{
return is_numeric($value)
? $this->where('id', $value)->firstOrFail()
: $this->where('slug', $value)->firstOrFail();
}