【问题标题】:Why I am getting this error in Laravel? Error:-Too few arguments to function Darryldecode\Cart\Cart::updateQuantityRelative()为什么我在 Laravel 中收到此错误?错误:-函数 Darryldecode\Cart\Cart::updateQuantityRelative() 的参数太少
【发布时间】:2020-08-22 05:47:06
【问题描述】:

我收到此错误:

参数计数错误 函数 Darryldecode\Cart\Cart::updateQuantityRelative() 的参数太少,第 261 行的 ..\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php 传入了 1 个,而预期的正是 3 个(查看:. ..\resources\views\cart.blade.php)

cart.blade.php

@foreach(\Cart::session(auth()->id())->getContent() as $items)
   <tr>
     <td class="text-center">
       <a href="{{ route('cart.destroy', $items->id)}}">x</a>
       </td>
        <td data-title="Product">
          <a href="#" class="text-gray-90">{{ $items ['name'] }}</a>
        </td>
       <td data-title="Price">
          <span class="">LKR {{ $items ['price'] }}.00</span>
       </td>
       <td data-title="Quantity">
            <span class="sr-only">Quantity</span>
       <!-- Quantity -->
               <div class="border rounded-pill py-1 width-122 w-xl-80 px-3 border-color-1">
                  <div class="js-quantity row align-items-center">
                      <div class="col">
                          <input class="js-result form-control h-auto border-0 rounded p-0 shadow-none" type="text" value="{{ \Cart::updateQuantityRelative($items->quantity) }}">
                        </div>
                         <div class="col-auto pr-1">
                              <a class="js-minus btn btn-icon btn-xs btn-outline-secondary rounded-circle border-0" href="javascript:;">
                                  <small class="fas fa-minus btn-icon__inner"></small>
                                </a>
                               <a class="js-plus btn btn-icon btn-xs btn-outline-secondary rounded-circle border-0" href="javascript:;">
                                    <small class="fas fa-plus btn-icon__inner"></small>
                                  </a>
                              </div>
                            </div>
                         </div> 
       <!-- End Quantity -->
      </td>
      <td data-title="Total">
          <span class="">
               {{ $items -> getPriceSum()}}
            </span>
        </td>
  </tr>
@endforeach

CartController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Product;
use App\Category;
use Darryldecode\Cart\Cart;

class CartController extends Controller
{
    public function index()
    {
        return view ('cart');
    }
   
    public function show($id)
    {
        $product = Product::find($id);
        return view('cart')->with(compact('product'));
    }
   
    public function destroy($itemId)
    {
       return back();
    }

    public function addtocart(Product $product)
    {
    
        \Cart::session(auth()->id())->add(array(
            'id' => $product->id,
            'name' => $product->prod_name,
            'price' => $product->prod_price,
            'quantity' => 1,
            'attributes' => array(),
            'associatedModel' => $product
        ));

        return redirect()->back();
    }
}

我安装了 Darryldecode 购物车包。

这个updateQuantityRelative函数在其中定义。

public function updateQuantityRelative($item, $key, $value)
{
    if (preg_match('/\-/', $value) == 1) {
        $value = (int)str_replace('-', '', $value);

        // we will not allowed to reduced quantity to 0, so if the given value
        // would result to item quantity of 0, we will not do it.
        if (($item[$key] - $value) > 0) {
            $item[$key] -= $value;
        }
    } elseif (preg_match('/\+/', $value) == 1) {
        $item[$key] += (int)str_replace('+', '', $value);
    } else {
        $item[$key] += (int)$value;
    }

    return $item;
}

我认为下面的代码需要更正。应该怎么样?

value="{{ \Cart::updateQuantityRelative($items->quantity) }}

【问题讨论】:

    标签: php laravel e-commerce


    【解决方案1】:

    我解决了这个问题。 添加了一个按钮作为更新购物车。

    <form action="{{route('cart.update',$items->id)}}">
                                                            <div class="d-md-flex">
                                                                <button type="submit" class="btn btn-soft-secondary mb-3 mb-md-0 font-weight-normal px-5 px-md-4 px-lg-5 w-100 w-md-auto">Update cart</button>
                                                                <a href="../shop/checkout.html" class="btn btn-primary-dark-w ml-md-2 px-5 px-md-4 px-lg-5 w-100 w-md-auto d-none d-md-inline-block">Proceed to checkout</a>
                                                            </div>
                                                        </form>
    

    并将总数更改为,

                                    <td data-title="Total">
                                        <span class="">
                                              {{Cart::session(auth()->id())->get($items->id)->getPriceSum()}}
                                        </span>
                                    </td>
    

    也纠正了这一点,

    <input class="js-result form-control h-auto border-0 rounded p-0 shadow-none" type="text" value="{{$items->quantity}}"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-12
      • 1970-01-01
      • 1970-01-01
      • 2022-12-13
      • 2015-11-03
      • 2011-10-08
      • 1970-01-01
      相关资源
      最近更新 更多