jeudi 21 février 2019

Laravel - get all items in a parent-category

I have the following table, called recipe_subcategories:

enter image description here

each recipe can be assigned to one parent category and multiple subcategories.

Currently, I have the following code to get the all recipes within a parent category:

public function recipes(Request $request) {
        $id = explode('-', $request->idName)[0];
        return Category::where('id',$id)->first()->recipes;
    }

The issue is, that if the same recipe belongs to different sub categories in the same parent category, it will return it multiple times.

What's the ideal solution for this issue? The goal is to return all recipes within one parent category (category_id) without duplicates.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire