简体   繁体   English

Laravel数组更新多行

[英]Laravel array to update multiple rows

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

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);

        }

that's your problem 那是你的问题

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

$data it'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? 这种方法更漂亮,不是吗?

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', ..]);

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 ,如果这需要花很长时间来看看Queing( https://laravel.com/docs/master/queues

Basically without knowing your data i would say remove [ID] . 基本上不知道您的数据,我会说删除[ID]

You dont need to know the ID. 您不需要知道ID。 remove it from everywhere in this function, 从此功能的任何地方删除它,

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的记录并对其进行更新。

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>

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM