dilfdo's avatar

Laravel eloquent with multiple where and orWhere clauses

below is my search query i want to search data to followinig critieria

cusine or mealname = $cusine and country = $country and block_seller = 0

but currently it doesnt check the where clauses after orWhere, which means country and block_seller where clauses are not working.

please advice

            $meals = Meals::select('*')
                ->selectRaw("( 6371 * acos( cos( radians($latitude) ) *
                                   cos( radians( latitude ) )
                                   * cos( radians( longitude ) - radians($longitude)
                                   ) + sin( radians($latitude) ) *
                                   sin( radians( latitude ) ) )
                                 ) AS distance")

                ->where('cuisine', 'LIKE', "%$cuisine%")
                ->orWhere('meal_name', 'LIKE', "%$cuisine%")
                ->where('country', "$country")
                ->where('blocked_seller', 0)
                ->with('Meals_images')
                ->orderBy('distance', 'ASC')
                ->get();
0 likes
5 replies
ChristineZedday's avatar

I had same problem, I have rearranged the order to have the orWhere after the where that applies to all:

$animaux = Animal::where('breeder_id', $seller->id)->where('sex', 'young male')->orWhere('sex', 'young female')->get();

previously: $animaux = Animal::where('sex', 'young male')->orWhere('sex', 'young female')->where('breeder_id', $seller->id)->get(); it didn't work, I had my young males once bought that where put for sale again each month! I don't now how to process with multiple orWhere.

ChristineZedday's avatar

And now, with the new order, that's the females that where re-put for sale! ;) Not really a progress, i suppose I had to write two queries... :(

ChristineZedday's avatar

$animaux = Animal::where('breeder_id', $seller->id)->where(function($query) { return $query->where('sex', 'young male')->orWhere('sex', 'jyoung female');})->get();

this work!

CliffordAtCaveoDotNL's avatar

Try grouping the where's for $cuisine:

$meals = Meals::select('*')
    ->selectRaw("( 6371 * acos( cos( radians({$latitude}) ) *
                                   cos( radians( latitude ) )
                                   * cos( radians( longitude ) - radians({$longitude})
                                   ) + sin( radians({$latitude}) ) *
                                   sin( radians( latitude ) ) )
                                 ) AS distance")
    ->where(static function ($query) use ($cuisine) {
        $query->where('cuisine', 'like', "%{$cuisine}%")
            ->orWhere('meal_name', 'like', "%{$cuisine}%");
    })
    ->where('country', '=' $country)
    ->where('blocked_seller', '=', 0)
    ->with('Meals_images')
    ->orderBy('distance', 'asc')
    ->get();
ChristineZedday's avatar

In fact, it was perfectly normal that it doesn't work, because AND has a greeter priority than OR! In a SQL request you should use parenthesis, and with where-> there's no parenthesis, so we need subqueries with functions.

Please or to participate in this conversation.