Model 多键查询
Laravel 模型类默认是通过id查询数据,通过复写模型类的 resolveRouteBinding() 方法实现既支持id又支持 slug 查询。
例如:
http://example.com/posts/1
http://example.com/posts/the-first-blog
public function resolveRouteBinding($value)
{
if (is_numeric($value)) {
return self::findOrFail($value);
} else {
return self::where('slug', $value)->firstOrFail();
}
}