使用类似
的链接http://example.com/catalog/slug/brand/product
和
之类的路线Route::get('catalog/{slug}/{brand}/{product}', 'Controller@method');
并且知道每个参数都有一个绑定,用于检查参数是否存在。
我有3张桌子:
Catalogs
id | catalog_slug
Brands
id | brand_slug
Products
id | product_slug | brand_id | catalog_id
如何检查这些参数是否相关(按产品表格)而不是它们是否仅存在于路线中?它工作正常,但我想确保它们彼此相关。
答案 0 :(得分:0)
在您的方法中添加此代码:
$parameters = Route::current()->parameters();
$areRelated = DB::table('Products')
->join('Brands','Brands.id','=','Products.brand_id')
->join('Catalogs','Catalogs.id','=','Products.catalog_id')
->where('Catalogs.catalog_slug',$parameters['slug'])
->where('Brands.brand_slug',$parameters['brand'])
->where('Products.product_slug',$parameters['product'])
->count();
if(! areRelated )
App::abort(404);
答案 1 :(得分:0)
Route::get('catalog/{catalog}/{brandByID}/{productByID}', 'Controller@method');
在bindings.php中(在globals.php的末尾创建并包含)
Route::model('catalog', 'Catalog'); // now using {catalog} in route will look up the catalog instance by ID and pass it to the controller method instead of the ID.
// use custom resolver for brand parameter, use {brandByID} in route
Route::bind('brandByID', function ($brandID, $route) {
//Get the catalog from the catalog bind function
$catalog = $route->parameter('catalog');
$brand = $catalog->brands()->findOrFail($brandID); // fails if catalog does not have this brand
return $brand;
}
为产品做同样的事情但添加第二张支票。您还需要在目录模型中正确设置品牌关系,例如
public function brands()
{
return $this->belongsToMany('Brand');
}