Laravel数组$request->all()为空

u7up0aaq  于 2022-09-28  发布在  iOS
关注(0)|答案(2)|浏览(301)

我正在和拉拉维勒一起做购物车。
我有:
路线:

Route::post('/panier/ajouter', 'CartController@store')->name('cart.store');
Route::patch('/panier/{product}', 'CartController@update')->name('cart.update');

视图:

<table class="table" id="table-shoppingcart">
                <thead>
                    <tr>
                        <th class="text-center" id="item-title-shoppingcart"></th>
                        <th class="text-center" id="size-title-shoppingcart">Taille</th>
                        <th class="text-center" id="quantity-title-shoppingcart">Quantité</th>
                        <th class="text-center" id="price-title-shoppingcart">Prix</th>
                        {{-- <th class="text-center" id="delete-title-shoppingcart"></th> --}}
                    </tr>
                </thead>
                <tbody>
                    @foreach (Cart::content() as $product)
                    <tr>

                        <th><img class="text-center item-content-shoppingcart" src="{{ $product->model->image }}"></th>
                        <td class="text-center td-table-shoppingcart size-content-shoppingcart">S</td>
                        <td class="td-table-shoppingcart quantity-content-shoppingcart">
                            <select name="quantity" class="custom-select text-center quantity" id="quantity" data-id="{{ $product->rowId }}">
                                @for ($i = 0; $i < 5 + 1 ; $i++)
                                <option id="quantity-option" {{ $product->qty == $i ? 'selected' : '' }}>{{ $i }}</option>
                            @endfor
                            </select>
                        </td>
                        <td class="text-center td-table-shoppingcart price-content-shoppingcart">{{ getPrice($product->subtotal()) }}</td>

                    </tr>
            @endforeach
                </tbody>
            </table>

Ajax请求:

$("#quantity").change(function(){
    const classname = document.querySelectorAll('#quantity')
    Array.from(classname).forEach(function(element) {
        console.log(element);
        let id = element.getAttribute('data-id')
        axios.patch(`/panier/${id}`, {
            quantity: this.value
        })
        .then(function (response) {
           // console.log(response);
           console.log("refresh");
            $("#table-shoppingcart").load(location.href + " #table-shoppingcart");
        })
        .catch(function (error) {
            console.log("erreur");
           // console.log(error);
        });
    })
});

我需要获取控制器中的数量,因此我尝试:

public function update(Request $request)
    {
        $data = $request->json()->all();
        Log::info($data);
        return response()->json(['success' => 'Cart Quantity Has Been Updated']);
    }

但当我尝试获取$data值时,我的数组在日志中是空的,如下所示:

[2020-02-22 10:49:46] local.INFO: array ()

我试图改变:

$data = $request->json()->all();

$data = $request->all();

但同样的问题。
你知道吗?
谢谢

abithluo

abithluo1#

Laravele用PUT/PATCH/DELETE等请求作弊。这些请求需要是e1d1e请求,并将额外变量'_method'设置为请求类型(例如)PATCH

vawmfj5a

vawmfj5a2#

因为HTML标准无法识别HTTP PUT。
您只需要添加POST类型的方法,但对于更新,您可以为PUT/PATCH类型的操作添加带有POST请求的小标志。

axios.post(`/panier/${id}`, { // <== use axios.post
 quantity: this.value,
 _method: 'patch'                   // <== add this field

})

相关问题