【问题标题】:Laravel 7.14 bulk update form button not workinLaravel 7.14 批量更新表单按钮不起作用
【发布时间】:2023-03-21 11:24:01
【问题描述】:

我在控制器中创建了一个批量更新功能,我将单个值保存在多行中。例如,用户输入 price = 30。相同的 30 将出现在 id 等于 [1,2,3,4,5,] 的行上。

在我的更新表单中,我有一个输入字段价格,如果我添加 value="{{ $cart->price }} 并使用新值(例如 85)发送更新请求。它采用之前的值,即 30。

如果我从输入中删除value="{{ $cart->price }},提交按钮将变为禁用。我也尝试将标签更改为。请帮助找出我做错了什么。

这是我的输入代码:

<div class="form-group">
<input type="text" name="price" value="" class="form-control" required>
</div>

这是我的控制器:

    public function bulkupdate(Request $request)
{
    // dd($request);

    $this->validate($request, [
        'id' => 'required',
        'currency_id' => 'required',
        'price' => 'required',
        'type_id' => 'required',
        'country' => '',
        'port' => '',
    ]);

    $ids = [$request->input('id')];
    $currency_id = $request->input('currency_id');
    $price = $request->input('price');
    $type_id = $request->input('type_id');
    $country = $request->input('country');
    $port = $request->input('port');
    $status = $request->input('status');

    // dd($price);

    foreach($ids[0] as $id) 
    {

        $cart = Cart::where('id', $id)->update([
            
            'status' => $status,
            'currency_id' => $currency_id,
            'price' => $price,
            'type_id' => $type_id,
            'country' => $country,
            'port' => $port,
        ]);

    }

    return redirect()->back()->with('success', 'Cart items updates');
}

【问题讨论】:

  • 你能发布完整的表格(至少是相关部分),尤其是有问题的提交按钮吗? “提交按钮变为禁用” 你的意思是它获得了disabled 属性?是否涉及任何 Javascript?

标签: php html mysql laravel forms


【解决方案1】:

首先你需要得到记录然后UPDATE它。
在您的代码中更改以下内容:

foreach($ids[0] as $id) 
{
    $cart = Cart::where('id', $id)->first()->update([ 
        'status' => $status,
        .
        .
        .
    ]);
}

【讨论】:

  • @NargeshRana 你的意思是你的代码无法到达Controller ???
  • 我的意思是当我点击提交按钮时,什么也没有发生。不知何故提交按钮被禁用。
  • 更新你的问题并添加你的刀片&lt;form&gt;标签。
【解决方案2】:

你不需要使用 foreach 循环,你可以通过 whereIn 来做到这一点:

您确定所有 id 都在 $ids 零索引上吗? 如果不是,你可以直接给 $ids 否则 $ids[0] 或 $ids 是一个数组,所以你直接在 whereIn 子句中传递 $ids

   Cart::whereIn('id', $ids)->update([
        
        'status' => $status,
        'currency_id' => $currency_id,
        'price' => $price,
        'type_id' => $type_id,
        'country' => $country,
        'port' => $port,
    ]);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-24
    • 2021-05-31
    • 1970-01-01
    • 1970-01-01
    • 2012-10-28
    • 1970-01-01
    • 1970-01-01
    • 2019-12-11
    相关资源
    最近更新 更多