将多个选定的行复选框值插入到 Laravel 数据库中。

huangapple go评论69阅读模式
英文:

Insert multiple selected rows checkbox values to database In Laravel

问题

I have a table that has three rows retrieved from SQL with foreach. I want to select two rows and insert them into another table in SQL.

The problem is that the code I wrote does not take the selected rows but the first two rows and inserts them into SQL.

英文:

I have a table that has three rows retrieved from SQL with foreach. I want to select two rows and insert them into another table in SQL.

The problem is that the code I wrote does not take the selected rows but the first two rows and inserts them into SQL.

将多个选定的行复选框值插入到 Laravel 数据库中。

<table id="myTable" class="table table-hover">
    <thead>
    <tr>
        <th width="1%">#</th>
        <th width="10%">Name product</th>
        <th width="40%">Price<span class="text-danger font-weight-bold">*</span></th>
        <th width="6%">Status <span class="text-danger font-weight-bold">*</span></th>
        <th width="1%"></th>
    </tr>
    </thead>
    <tbody>
    @php($count = 0)
    @foreach($product as $row)
        @php($count++)
        <tr>
            <td><span>{{$count}}</span></td>
            <td><input type="text" name="product_name[]" value="{{ $row->product_name }}" class="form-control" readonly></td>
            <td><input type="number" name="product_rate[]" value="{{ $row->product_rate }}" class="form-control" readonly></td>
            <td><input type="text" name="product_status[]" value="{{ $row->product_status }}" class="form-control" readonly></td>
            <td><input type="checkbox" name="selectedRows[]" value="{{$count}}"></td>
        </tr>
    @endforeach
    </tbody>
</table>

<?php
public function newProductTable(Request $request)
{
    $selectedRows = $request->input('selectedRows', []);

    if (!$selectedRows) {
        return redirect()->back()->with('error', 'Error');
    } else {
        $current_date = date('Y-m-d H:i:s');
        {
            $selectedRows = $request->input('selectedRows', []);

            foreach ($selectedRows as $count) {
                DB::table('new_product_table')->insert([
                    'product_name' => $request->product_name[$count],
                    'product_rate' => $request->product_rate[$count],
                    'product_status' => $request->product_status[$count],
                    'created_at' => $current_date,
                    'updated_at' => $current_date,
                ]);
            }
        }

        return redirect()->back()->with('success', 'Successfully');
    }
}
?>

答案1

得分: 1

将复选框的值设置为标识符,即每行的ID。

<td><input type="checkbox" name="selectedRows[]" value="{{ $row->id }}"></td>

更新后的代码

$selectedRows = $request->input('selectedRows', []);

foreach ($selectedRows as $productId) {
    $product = DB::table('original_table')->where('id', $productId)->first();

    DB::table('new_product_table')->insert([
        'product_name' => $product->product_name,
        'product_rate' => $product->product_rate,
        'product_status' => $product->product_status,
        'created_at' => $current_date,
        'updated_at' => $current_date,
    ]);
}

'original_table'替换为从中检索所选行的适当表名。

英文:

Set the value of the checkboxes to the identifier, in this case, the id of each row.

&lt;td&gt;&lt;input type=&quot;checkbox&quot; name=&quot;selectedRows[]&quot; value=&quot;{{ $row-&gt;id }}&quot;&gt;&lt;/td&gt;

Updated code

$selectedRows = $request-&gt;input(&#39;selectedRows&#39;, []);

foreach ($selectedRows as $productId) 
{
    $product = DB::table(&#39;original_table&#39;)-&gt;where(&#39;id&#39;, $productId)-&gt;first();

    DB::table(&#39;new_product_table&#39;)-&gt;insert([
        &#39;product_name&#39; =&gt; $product-&gt;product_name,
        &#39;product_rate&#39; =&gt; $product-&gt;product_rate,
        &#39;product_status&#39; =&gt; $product-&gt;product_status,
        &#39;created_at&#39; =&gt; $current_date,
        &#39;updated_at&#39; =&gt; $current_date,
    ]);
}

Substitute 'original_table' with the appropriate table name from which you are retrieving the selected rows.

huangapple
  • 本文由 发表于 2023年5月30日 03:19:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76359875.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定