Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
943 B
2
Indexable
private function createCart($resId){
        return $cart = Cart::create([
            'user_id' => auth()->user()->id,
            'restaurant_id' => $resId,
            'is_payed' => '0'
        ]);
    } 
    private function findCart($resId){
       return Cart::query()->where('user_id', auth()->user()->id)
            ->where('restaurant_id', $resId)->where('is_payed', '0')->first();
    }
    private function getOrCreateCarts(Restaurant $restaurant)
    {
        $cart=$this->findCart($restaurant->id);
        if (!$cart)
            return $this->createCart($restaurant->id);
        return $cart;
    }
    public function store(Request $request, Restaurant $restaurant)
    {

        $cart = $this->getOrCreateCarts($restaurant);

        $cart->foods()->attach($request->get('food_id'), ['count' => $request->get('count')]);

        return response()->json(['message' => 'added to cart successfully', 'data' => $cart]);
    }