Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel whereNotNull statment not working properly

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 Answers

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'));

}
like image 83
aynber Avatar answered Apr 08 '26 16:04

aynber



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!