ysfkaya
ysfkaya

Reputation: 398

Laravel findOrFail method for multiple table

There is two tables categories and category types. I'm trying get category by slug and category type with id. How can I check whether these two tables and data exists ?

My code:

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'));
    }

Upvotes: 0

Views: 1716

Answers (1)

Rahman Qaiser
Rahman Qaiser

Reputation: 672

Try this:

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

Upvotes: 1

Related Questions