我试图用蛞蝓的方式来显示我的URL链接。例如,而不是使用:
http://localhost:8000/product/1
我想用...
http://localhost:8000/product/blue-shirts
到目前为止,我设法通过封装cviebrock/eloquent-sluggable
添加蛞蝓。我应该用我的路线和看法?
<?php
use Sluggable;
public function sluggable()
{
return [
'slug' => [
'source' => 'product_name'
]
];
}
您可以自定义路径模型,在RouteServiceProvider
约束力的决议逻辑:
public function boot()
{
parent::boot();
Route::bind('product', function ($value) {
return Product::where('slug', $value)->first() ?? abort(404);
});
}
见the docs here以获取更多信息。
另外,以下内容添加到您的Product
模型使用隐式绑定:
/**
* Get the route key for the model.
*
* @return string
*/
public function getRouteKeyName()
{
return 'slug';
}