1

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.

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.

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
2
  • can you show your $data after this line $data = $request->except(['_token']); Commented May 16, 2017 at 7:02
  • array:8 [▼ "id" => array:10 [▶] "Channel" => array:10 [▶] "Posts" => array:10 [▶] "Monthly_Admin_Fee" => array:10 [▶] "Legal_Fee" => array:10 [▶] "Valuation_Fee" => array:10 [▶] "Rate" => array:10 [▶] "Mortgage_Risk_Fee" => array:10 [▶] ] Commented May 16, 2017 at 7:54

6 Answers 6

2

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

        }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Divyank. This looks like it should work! I'm still getting an undefined index error though for whats inside of the $input array. I believe it could be because it's an array inside of an array/nested array? :S
1

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'

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?

8 Comments

Thank you. I've just tried with the foreach loop in your last example and I'm still getting a undefined index:id on line where('id', $row['id]) When I dd($data) I can see the ID inside of the array which appears like this array:8 [▼ "id" => array:10 [▼ 0 => "" 1 => "" 2 => "" 3 => "" 4 => "" 5 => "" 6 => "" 7 => "" 8 => "" 9 => ""
Thank you huuuk! that makes sense now from going through it. legend :)
Sorry just have one more question huuuk, I really like what you have said, just the first line in the controller function is causing errors, I've added the image in my OP to better explain.
Parse error on line for($request->get('members', []) as $member) $member is undefined, and I'm hesitant about defining $member before the loop and reassigning it. I've added the image of the line in the post now.
Sorry for typo, chage for to foreach
|
0

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

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)

Comments

0

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

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

Comments

0

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//

$a_ids = array(....);

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

Comments

0

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

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

EDIT

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

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

1 Comment

I think in this case the DB::table('membership')->where('id', $row['id'])->update($input) Is pretty important because If I edit a row with the id of 12 and another with the id of 9. It would need to update both of those rows?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.