Advertisement
Qpel

val egz

Mar 19th, 2020
484
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.98 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\DB;
  7. use App\Http\Controllers\Controller;
  8. use \App\User;
  9. use \App\Food;
  10.  
  11. interface PagesInterface
  12. {
  13.     public function index();
  14.     public function food();
  15.     public function addToCart($id);
  16.     public function update(Request $request);
  17.     public function remove(Request $request);
  18. }
  19.  
  20. class PagesController extends Controller implements PagesInterface
  21. {
  22.  
  23.     public function index(){
  24.        $users = User::all();
  25.         return view('pages.index', ['users' => $users]);
  26.     }
  27.     public function food(){
  28.         $foods = Food::all();
  29.          return view('food.index', ['foods' => $foods]);
  30.      }
  31.  
  32.      public function addToCart($id)
  33.     {
  34.         $food = Food::find($id);
  35.  
  36.         if(!$food) {
  37.  
  38.             abort(404);
  39.  
  40.         }
  41.         $cart = session()->get('cart');
  42.         // if cart is empty then this the first product
  43.         if(!$cart) {
  44.  
  45.             $cart = [
  46.                     $id => [
  47.                         "name" => $food->name,
  48.                         "quantity" => 1,
  49.                         "price_big" => $food->price_big,
  50.                         "image" => $food->image
  51.                     ]
  52.             ];
  53.  
  54.             session()->put('cart', $cart);
  55.  
  56.             return redirect()->back()->with('success', 'Product added to cart successfully!');
  57.         }
  58.  
  59.         // if cart not empty then check if this product exist then increment quantity
  60.         if(isset($cart[$id])) {
  61.  
  62.             $cart[$id]['quantity']++;
  63.  
  64.             session()->put('cart', $cart);
  65.  
  66.             return redirect()->back()->with('success', 'Product added to cart successfully!');
  67.  
  68.         }
  69.  
  70.         // if item not exist in cart then add to cart with quantity = 1
  71.         $cart[$id] = [
  72.             "name" => $food->name,
  73.             "quantity" => 1,
  74.             "price_big" => $food->price_big,
  75.             "image" => $food->image
  76.         ];
  77.  
  78.         session()->put('cart', $cart);
  79.  
  80.         return redirect()->back()->with('success', 'Product added to cart successfully!');
  81.     }
  82.    
  83.     public function update(Request $request)
  84.         {
  85.             if($request->id and $request->quantity)
  86.             {
  87.                 $cart = session()->get('cart');
  88.    
  89.                 $cart[$request->id]["quantity"] = $request->quantity;
  90.    
  91.                 session()->put('cart', $cart);
  92.    
  93.                 session()->flash('success', 'Cart updated successfully');
  94.             }
  95.         }
  96.    
  97.         public function remove(Request $request)
  98.         {
  99.             if($request->id) {
  100.    
  101.                 $cart = session()->get('cart');
  102.    
  103.                 if(isset($cart[$request->id])) {
  104.    
  105.                     unset($cart[$request->id]);
  106.    
  107.                     session()->put('cart', $cart);
  108.                 }
  109.    
  110.                 session()->flash('success', 'Product removed successfully');
  111.             }
  112.         }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement