Home > Blockchain >  Filter field returns all records when search is empty
Filter field returns all records when search is empty

Time:12-22

When I don't insert anything in the search field, all the records in my table return, I don't want that to happen, how can I do so that it doesn't return any results if $search = $request->input('search'); is empty?

    public function search(Request $request)
    {
        $search = $request->input('search');
        $client = ClientModel::where('name','LIKE',"%{$search}%")->get();
        return view('search', compact('client'));
    }

How to solve this problem?

CodePudding user response:

    public function search(Request $request)
    {
        $client = array();
        if($request->filled('search')){
          $search = $request->input('search');
          $client = ClientModel::where('name','LIKE',"%{$search}%")->get(); 
        }
        return view('search', compact('client'));
    }

Please use fillled method to check the data present in the GET Super Global variable if data is there then run your query otherwise return an empty array.

Thanks

CodePudding user response:

public function search(Request $request)
    {
        $search = $request->input('search');
        if(!empty($search)){
          $client = ClientModel::where('name','LIKE',"%{$search}%")->get(); 
          return view('search', compact('client'));
        }else{
           return view('search', compact('client'));
        }
    }
  • Related