Di keranjang belanja laravel saya, saya membuat fungsi hapus, dan tampaknya baik-baik saja saat menguji dengan fungsi json atau dd, tetapi di mana ada masalah ketika mencoba mengembalikan tampilan keranjang rute Route [cart] not defined.. Fungsi tersebut juga perlu dioptimalkan ketika saya ingin menghapus hanya 1 item dari produk yang sama, yang berarti diperlukan "Hapus 1 item" dan "Hapus semua item" per baris.

Model keranjang:

class Cart 
{
   public $items = null;
   public $totalQty = 0;
   public $totalPrice = 0;

   public function __construct($oldCart)
   {
     if ($oldCart) {
        $this->items = $oldCart->items;
        $this->totalQty = $oldCart->totalQty;
        $this->totalPrice = $oldCart->totalPrice;
     }
   }

   public function add($item, $id)
   {
     $storedItem = ['qty' => 0, 'price' => $item->price, 'item' => $item, 'imagePath' => $item->imagePath];

     if ($this->items ) {

            if (array_key_exists($id, $this->items)) {
                 $storedItem = $this->items[$id];
             }
         }
         $storedItem['qty']++;
         $storedItem['price'] = $item->price * $storedItem['qty'];
         $storedItem['imagePath'] = $item->imagePath;
         $this->items[$id] = $storedItem;
         $this->totalQty++;
         $this->totalPrice += $item->price;
    }

    public function delete($id) {

        if(!$this->items || !isset($this->items[$id])) {
            return false;  // maybe throw an exception here?
        }
data of $this->items, create a method to calculate the totals when needed.
        $this->totalQty -= $this->items[$id]['qty'];
        $this->totalPrice -=  $this->items[$id]['price'] * $this->items[$id]['qty'];

        // and remove the item
        unset($this->items[$id]);
    }
}

Pengontrol keranjang:

class ProductController extends Controller

{
    /**
    *@return \Illuminate\Http\Response
    */
    public function index()
    {
        $products = Product::all();
        return view('home', ['products'=> $products]);
    }

    public function getAddToCart(Request $request, $id) 
    {
        $product = Product::find($id);
        $oldCart = Session::has('cart') ? Session::get('cart') : null;
        $cart = new Cart($oldCart);
        $cart->add($product, $product->id);
        
        $request->session()->put('cart', $cart);
        return redirect()->route('home');
    }

    public function getCart()
    {
        if (!Session::has('cart')) {
            return view('cart');
        }
        $oldCart = Session::get('cart');

        $cart = new Cart($oldCart); 
        return view('cart', ['products' => $cart->items, 'totalPrice' => $cart->totalPrice]);
    }

    /**
     * Remove the specified resource from storage.
     * @param int $id
     * @return \Illuminate\Http\Request
     */
   public function destroy(Request $request, $id) {
        $oldCart = Session::has('cart') ? Session::get('cart') : null;
        $cart = new Cart($oldCart);
        $cart->delete($id);
        $request->session()->put('cart', $cart);
        return redirect()->route('cart');

        
        //return response()->json(array( 'totalqty' => $cart->totalQty, 'totalPrice' => $cart->totalPrice));
        //{"totalqty":0,"totalPrice":-222000}
    }

}

Tampilan keranjang:

<form method="POST" action="{{ route('cart.destroy', ['id' => $product['item']['id']]) }}" > 
            @csrf
            {{ method_field('DELETE') }}
            <input type="submit" value="Delete all items" onclick="return confirm('Are you sure?')" class="btn btn-danger" />
            <input type="submit" value="Delete 1 item" onclick="return confirm('Are you sure?')" class="btn btn-danger" />
      </form>
0
Gazmir Sulcaj 3 Juli 2020, 05:17

1 menjawab

Jawaban Terbaik

Saya memecahkan masalah dengan kereta rute dari return redirect()->route('cart'); ke return redirect()->route('shoppingCart');.

0
Gazmir Sulcaj 3 Juli 2020, 02:23