Untitled
unknown
plain_text
3 years ago
943 B
10
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]);
}Editor is loading...