how to apply keyword searching in child relationship?

31 Views Asked by At

i want to apply keyword searching on products that is child of categories table. categories table is parent and products table is child and both having hasMany relationship.

$terms = explode(" ", $req->searchInput);

                $result= $categories->query()
                ->whereHas('products', function ($query) use ($terms) {
                    foreach ($terms as $term) {
                        // Loop over the terms and do a search for each.
                        $query->where('name', 'like', '%' . $term . '%');
                    }
                    return $query;
                })->get();
0

There are 0 best solutions below