mardi 27 mars 2018

Laravel multiple where clause

I was trying to get list of foods which are in cart as well as favorites table. I used the following Query to access it. In Conditional Clauses section i wanted to check if the food is in favorites table it also shows or if it's in carts table also will be shown. But it can't check inside the where clause means the foods id is in the carts table but not in that user. So it should return cart_id as null but it shows cart_id though that user didn't add into his carts table. Same happened in favorites table. How can i make it correct?

$foods = DB::table('foods')
             ->leftjoin('carts','carts.food_id','foods.id')
             ->leftjoin('favorites','favorites.food_id','foods.id')
             ->select('foods.food_name', DB::raw('carts.id as cart_id'),DB::raw('favorites.id as favorite_id'),'foods.id','foods.display_image','foods.price','foods.category_id','foods.description','foods.restaurant_id' )
             ->where('foods.restaurant_id','=',$request->Input(['restaurant_id']))    
                ->orwhere(function ($query) {
                            $query->where('carts.user_id','=',Auth::user()->id)
                                  ->where('favorites.user_id','=',Auth::user()->id);
                    })  

               ->get();



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire