有两种表类别和类别类型。我试图通过slug和类别类型获取类别。如何检查这两个表和数据是否存在?
我的代码:
public function typeIndex($slug, $typeId)
{
$category = Category::findBySlug($slug)->with('types')->whereHas('types',function(Builder $builder) use ($typeId){
return $builder->where('id',$typeId);
});
return view('frontend.category.index', compact('category'));
}
答案 0 :(得分:0)
试试这个:
$category = Category::whereHas('types' , function($query) use($typeId){
$query->where('id' , $typeId);
})->where('slug' , $slug)->firstOrFail();