Laravel 数组更新多行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43994271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:58:15  来源:igfitidea点击:

Laravel array to update multiple rows

phplaravellaravel-5laravel-5.2

提问by n31l

I've done a ton of looking through older posts and no luck yet, I'm trying to update multiple rows within a table from a single form with the ID being the primary key.

我已经翻阅了大量旧帖子,但还没有运气,我试图从一个表单中更新表中的多行,ID 是主键。

The contents are being displayed in what looks like a spreadsheet where the user can edit multiple rows.

内容显示在看起来像电子表格的电子表格中,用户可以在其中编辑多行。

I'm getting an undefined index: ID error.

我得到一个未定义的索引:ID 错误。

The code I'm using bellow seems really close though something isn't right.

尽管有些不对劲,但我在下面使用的代码似乎非常接近。

If anyones done this before and can correct this code your help would be greatly appreciated!

如果任何人之前做过这件事并且可以更正此代码,您的帮助将不胜感激!

Thank you.

谢谢你。

protected function updateMultiple(Request $request)

{

    $data = $request->except(['_token']);

    //  dd($data);
    for($i = 0; $i <= count($data['id']); $i++) {

        $input = [
            'id' => $data['id'][$i],
            'Channel' => $data['Channel'][$i],
            'Posts' => $data['Posts'][$i],
            'Monthly_Admin_Fee' => $data['Monthly_Admin_Fee'][$i],
            'Legal_Fee' => $data['Legal_Fee'][$i],
            'Valuation_Fee' => $data['Valuation_Fee'][$i],
            'Mortgage_Risk_Fee' => $data['Mortgage_Risk_Fee'][$i],
        ];

        DB::table('membership')->update($input);

    }

}

View

看法

        @foreach($members as $member)
            <tr>
                <td class="text-right">
                    <input type="text" style="padding-right: 8px;padding-left: 8px;" name="id[]" id="id">{{$member->id}}</td>
                <td><input type="text" id="Channel" name="Channel[]" class="form-control" value="{{$member->Channel}}"></td>
                <td><input type="text" id="Posts" name="Posts[]" class="form-control" value="{{$member->Posts}}"></td>
                <td><input type="text" id="Monthly_Admin_Fee" name="Monthly_Admin_Fee[]" class="form-control" value="{{$member->Monthly_Admin_Fee}}"></td>
                <td><input type="text" id="Legal_Fee" name="Legal_Fee[]" class="form-control" value="{{$member->Legal_Fee}}"></td>
                <td><input type="text" id="Valuation_Fee" name="Valuation_Fee[]" class="form-control" value="{{$member->Valuation_Fee}}"></td>
                <td><input type="text" id="Mortgage_Risk_Fee" name="Mortgage_Risk_Fee[]" class="form-control" value="{{$member->Mortgage_Risk_Fee}}"></td>
            </tr>
        @endforeach

回答by Divyank Munjapara

This will do the job

这将完成工作

view

看法

@foreach($members as $member)
    <tr>
        <td class="text-right">
            <input type="text" style="padding-right: 8px;padding-left: 8px;" name="id[{{$member->id}}]" id="id">{{$member->id}}</td>
        <td><input type="text" id="Channel" name="Channel[{{$member->id}}]" class="form-control" value="{{$member->Channel}}"></td>
        <td><input type="text" id="Posts" name="Posts[{{$member->id}}]" class="form-control" value="{{$member->Posts}}"></td>
        <td><input type="text" id="Monthly_Admin_Fee" name="Monthly_Admin_Fee[{{$member->id}}]" class="form-control" value="{{$member->Monthly_Admin_Fee}}"></td>
        <td><input type="text" id="Legal_Fee" name="Legal_Fee[{{$member->id}}]" class="form-control" value="{{$member->Legal_Fee}}"></td>
        <td><input type="text" id="Valuation_Fee" name="Valuation_Fee[{{$member->id}}]" class="form-control" value="{{$member->Valuation_Fee}}"></td>
        <td><input type="text" id="Mortgage_Risk_Fee" name="Mortgage_Risk_Fee[{{$member->id}}]" class="form-control" value="{{$member->Mortgage_Risk_Fee}}"></td>
    </tr>
@endforeach

controller

控制器

foreach ($request->all() as $key => $data ) {

            $input = [
                'id' => $data['id'][$key],
                'Channel' => $data['Channel'][$key],
                'Posts' => $data['Posts'][$key],
                'Monthly_Admin_Fee' => $data['Monthly_Admin_Fee'][$key],
                'Legal_Fee' => $data['Legal_Fee'][$key],
                'Valuation_Fee' => $data['Valuation_Fee'][$key],
                'Mortgage_Risk_Fee' => $data['Mortgage_Risk_Fee'][$key],

            ];
            DB::table('membership')->where('id',$key)->update($input);

        }

回答by huuuk

that's your problem

那是你的问题

for($i = 0; $i <= count($data['id']); $i++) {` 

$datait's array of arrays, it doesn't have element with key 'id'

$data它是数组数组,它​​没有带键的元素 'id'

for($i = 0; $i <= count($data); $i++) {

or even better

甚至更好

foreach ($data as $row) {
    $desired_keys = [
        'Channel',
        'Posts',
        'Monthly_Admin_Fee',
        'Legal_Fee',
        'Valuation_Fee',
        'Mortgage_Risk_Fee',
    ];
    $input = array_only($row, $desired_keys);
    BD::table('membership')->where('id', $row['id'])->update($input)
}


EDIT

编辑

Sorry, I've missled you. Because of your approach.
Can I suggest you this one?

对不起,我误会了你。因为你的方法。
我可以给你推荐这个吗?

@foreach($members as $i => $member)
    <tr>
        <td class="text-right">
            <input type="text" style="padding-right: 8px;padding-left: 8px;" name="members[{{ $i }}][id]" id="id">{{$member->id}}
        </td>
        <td>
            <input type="text" id="Channel" name="members[{{ $i }}][Channel]" class="form-control" value="{{$member->Channel}}">
        </td>
        <td>
            <input type="text" id="Posts" name="members[{{ $i }}][Posts]" class="form-control" value="{{$member->Posts}}">
        </td>
        <td>
            <input type="text" id="Monthly_Admin_Fee" name="members[{{ $i }}][Monthly_Admin_Fee]" class="form-control" value="{{$member->Monthly_Admin_Fee}}">
        </td>
        <td>
            <input type="text" id="Legal_Fee" name="members[{{ $i }}][Legal_Fee]" class="form-control" value="{{$member->Legal_Fee}}">
        </td>
        <td>
            <input type="text" id="Valuation_Fee" name="members[{{ $i }}][Valuation_Fee]" class="form-control" value="{{$member->Valuation_Fee}}">
        </td>
        <td>
            <input type="text" id="Mortgage_Risk_Fee" name="members[{{ $i }}][Mortgage_Risk_Fee]" class="form-control" value="{{$member->Mortgage_Risk_Fee}}">
        </td>
    </tr>
@endforeach

Then in controller

然后在控制器中

protected function updateMultiple(Request $request)
{
    foreach($request->get('members', []) as $member) {
        DB::table('membership')->where('id', $member['id'])
            ->update(array_except($member, ['id']))
    }
}

This approach is much more pretty, isn't it?

这种方法更漂亮,不是吗?

回答by Doan Tran

you can follow like example :

你可以按照这样的例子:

$data_upate = array(
  array(
    'field1'=>'value1',
    'field2'=>'value2',
    ...
  ),
  array(
    'field1'=>'value1',
    'field2'=>'value2',
    ...
  ),
);

// list id of record you want to update//

// 要更新的记录的列表 id //

$a_ids = array(....);

$a_ids = array(....);

DB::table('table_name')->whereIn('id',$a_ids)->update($data_update);

DB::table('table_name')->whereIn('id',$a_ids)->update($data_update);

回答by pascalvgemert

You cannot update multiple rows with different data with one query like the insert()method.

您不能像insert()方法一样使用一个查询更新具有不同数据的多行。

When the data is the same for all rows that need to be updated, you can use:

当需要更新的所有行的数据都相同时,可以使用:

DB::table('membership') ->whereIn('id', [1,2,3,4]) ->update(['Channel' => 'your-value', ..]);

DB::table('membership') ->whereIn('id', [1,2,3,4]) ->update(['Channel' => 'your-value', ..]);

Else you will need to use a foreach, if this takes to long take a look at Queing (https://laravel.com/docs/master/queues)

否则,您将需要使用foreach,如果这需要很长时间,请查看队列(https://laravel.com/docs/master/queues

回答by Onix

Basically without knowing your data i would say remove [ID].

基本上不知道你的数据我会说 remove [ID]

You dont need to know the ID. remove it from everywhere in this function,

您不需要知道 ID。从这个函数的任何地方删除它,

EDIT

编辑

Just change your query to: DB::table('membership')where(id, $data['id'][$i] )->update($input);

只需将您的查询更改为: DB::table('membership')where(id, $data['id'][$i] )->update($input);

this will fetch the record with the current id and update it.

这将使用当前 id 获取记录并更新它。

回答by Autista_z

Mistake is in view. You have forgot to insert value to ID input.

错误在眼中。您忘记在 ID 输入中插入值。

<input type="text" style="padding-right: 8px;padding-left: 8px;" name="id[]" id="id" value="{{$member->id}}">{{$member->id}}</td>