4

I fetching data from DB and when I do that, trying to eliminate null rows.

public function search(Request $request)
{
    $q = $request->q;

    $estates = \DB::table('allestates')
        ->whereNotNull('lat')
        ->whereNotNull('lng')
        ->where('lat', '!=', '')
        ->where('log', '!=', '')
        ->where(function($query) {
            $query->where("building_name", "LIKE", "%" . $q . "%")
                ->orWhere("address", "LIKE", "%" . $q . "%")
                ->orWhere("company_name", "LIKE", "%" . $q . "%")
                ->orWhere("region", "LIKE", "%" . $q . "%")
        })
        ->orderBy('price')->paginate(8);

    return view("home", compact('estates', 'q'));
}

I can't figure out how to solve syntax error at this line:

   })
   ->orderBy('price')->paginate(8);

1 Answers1

1

You can check for empty strings by adding ->where('lat', '!=', '')->where('log', '!=', '') to your query. You'll also need to add a closure to break off your OR statements:

public function search(Request $request)
{
    $q = $request->q;

    $estates = \DB::table('allestates')
        ->whereNotNull('lat')
        ->whereNotNull('lng')
        ->where('lat', '!=', '')
        ->where('lng', '!=', '')
        ->where(function($query) use ($q) {
            $query->where("building_name", "LIKE", "%" . $q . "%")
                ->orWhere("address", "LIKE", "%" . $q . "%")
                ->orWhere("company_name", "LIKE", "%" . $q . "%")
                ->orWhere("region", "LIKE", "%" . $q . "%");
        })

        ->orderBy('price')->paginate(8);

    return view("home", compact('estates', 'q'));

}
aynber
  • 22,380
  • 8
  • 50
  • 63
  • thank you for answer. it's working but, how can I solve the syntax error at this line `}) ->orderBy('price')->paginate(8);` –  Dec 14 '18 at 16:40
  • Sorry, forgot a semi-colon. I also forgot to pass in the $q so the subqueries could use it. – aynber Dec 14 '18 at 16:42
  • @Barbie change `->where('log', '!=', '')` to `->where('lng', '!=', '')` – Remul Dec 14 '18 at 16:46
  • Spelling error, `log` should be `lng` in the second new `->where()` clause. – Tim Lewis Dec 14 '18 at 16:46