samedi 20 avril 2019

Laravel query not working with multiple where

I have to implement the search functionality. As shown below I have one search field which is text and it will search through name,description and budget. And other search fields like cid, status. So, when I use only search or only cid field to search its working fine but when I use all of them together(search,cid,status) its not working fine then its only searching through search field. Don't know where is the problem. Following is my code:

        $projects = Project::where(['is_active'=>1])
                    ->with('customer')
                    ->withCount(['inactiveTask', 'activeTask', 
                    'completeTask']);

       if(@$request->get('search')!="")
        {
          $search = $request->get('search');
          $projects = $projects ->Where('name', 'LIKE', "%{$search}%")
                       ->orWhere('description', 'LIKE', "%{$search}%")
                       ->orWhere('budget', 'LIKE', "%{$search}%");
        } 
        if(@$request->get('cid')!="")
        {
          $projects = $projects->where(['customer_id'=>$request->get('cid')]);
        }
        if(@$request->get('status')!="")
        {
          $projects = $projects->where(['status'=>$request->get('status')]);
        }
        if(@$request->get('budget-from')!="" && @$request->get('budget-to')!="")
        {
        $projects = $projects->whereBetween('budget', [$request->get('budget-from'), $request->get('budget-to')]);
        }
        $projects->get();



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire