samedi 29 août 2020

Laravel | Unable to get the search results filtered for *multiple keywords* | Returns all from the table now

Code(Controller):

    public function index(Request $request)
    { 
      
     
        try{
            $this->productRepository->pushCriteria(new RequestCriteria($request));
            $this->productRepository->pushCriteria(new LimitOffsetCriteria($request));
            $this->productRepository->pushCriteria(new ProductsOfFieldsCriteria($request));
            if($request->get('trending',null) == 'week'){
                $this->productRepository->pushCriteria(new TrendingWeekCriteria($request));
            }
            else{
                $this->productRepository->pushCriteria(new NearCriteria($request));
            }

          $queryString = $request->query;
  
        if ($queryString = $request->query('search')) {
       //     [$column, $term] = explode(':', $queryString);

       $terms = explode(" ", request('q'));

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

else
        
           $products = $this->productRepository->all(); 
    
       
        } catch (RepositoryException $e) {
            return $this->sendError($e->getMessage());
        }

        return $this->sendResponse($products->toArray(), 'Products retrieved successfully');
    }

I'm getting the complete set of data within the product table as of now. My intension is to be able to filter the results with matching keywords as in $terms . Can someone please help figure out what's missing in filtering above ?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3jp5otx
via IFTTT

Aucun commentaire:

Enregistrer un commentaire