filtering the model based on a relationship laravel

  eloquent, laravel, php

So my code is

$categories=Category::where('user_id', $store->id)->whereHas('childrenCategories', function($q) use ($id){
            $q->where('user_id', $id);
        })->orwhereHas('products', function($q) use ($id) {
            $q->where('auth_id', $id);
        })->with('products', 'childrenCategories')->latest()->get();

I want to get all children categories with and products with given id but this code doesn’t seem to work. As children categories with user_id other than id are also being returned. Sorry, I am relatively new to Laravel. And I thought this would be a good platform to ask. Also, I can share the relationships if you want me to.

Source: Laravel

Leave a Reply