Laravel findOrFail方法为多个表

时间:2018-04-04 11:39:40

标签: php laravel

有两种表类别和类别类型。我试图通过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'));
    }

1 个答案:

答案 0 :(得分:0)

试试这个:

$category = Category::whereHas('types' , function($query) use($typeId){
    $query->where('id' , $typeId);
})->where('slug' , $slug)->firstOrFail();