ALI ISMAEEL
ALI ISMAEEL

Reputation: 115

Laravel - update multiple rows in a table

I am trying to update one column (S_Rank) in all the available rows. I am listing the rows in a table, inside the table I have drop down menu to change the rank. I want to be able to save all the new rankings. I have two issues, the first one, the submit button doesn't work outside the <td> </td>and I can't put the submit button inside the <td> because then it will be listed in each row. My second issue I am not sure how to save all the changes to the database

The current issue is that I can't make the button work outside the <td></td> tags, can anyone help

In my view

<table id="myTable" class ="table table-striped">

<thead>
        <td><h4>Student Name</h4></td>
        <td><h4>Student Rank</h4></td>
</thead>

@foreach($applications as $application)
<tbody>
<tr>

<td><h5>{{$application->Student_Name}}</h5></td>

<td><h5>

{!! Form::open(['action' => 'AbstractsController@UpdateRank' , 'method' => 'post' ]) !!}
{{Form::select('Ranking' ,$ranks,  ['class' => 'form-control', 'placeholder' =>$application->S_Rank] )}} 
{{Form::hidden('Application_ids[]',  $application->S_ID)}}

</h5></td>

</tr>

@endforeach

</tbody>
</table>

{{Form::Submit('Save New Ranking',['class' => 'btn btn-primary'])}}
{!! Form::close() !!}

In my controller

foreach(request('Application_ids') as $A_ID){
    $Application= Application::find($A_ID);
    $Application->S_Rank = 3;
    $Application->save();
          }

Upvotes: 0

Views: 697

Answers (3)

Kyle Wardle
Kyle Wardle

Reputation: 830

Move the

{!! Form::open(['action' => 'AbstractsController@UpdateRank' , 'method' => 'post' ]) !!}

To outside of the loop and the table. You only want to define this once.

{!! Form::open(['action' => 'AbstractsController@UpdateRank' , 'method' => 'post' ]) !!}

<table id="myTable" class ="table table-striped">

<thead>
        <td><h4>Student Name</h4></td>
        <td><h4>Student Rank</h4></td>
</thead>

<tbody>
@foreach($applications as $application)
<tr>

<td><h5>{{$application->Student_Name}}</h5></td>

<td><h5>

{{Form::select('Ranking' ,$ranks,  ['class' => 'form-control', 'placeholder' =>$application->S_Rank] )}} 
{{Form::hidden('Application_ids[]',  $application->S_ID)}}

</h5></td>

</tr>

@endforeach

</tbody>
</table>

{{Form::Submit('Save New Ranking',['class' => 'btn btn-primary'])}}
{!! Form::close() !!}

Upvotes: 0

Dan
Dan

Reputation: 5348

Disclaimer: This code's not tested nor is it the most effective way to solve this. It's is only a waypoint to point out the issue.

View

{!! Form::open(['action' => 'AbstractsController@updateRank' , 'method' => 'post' ]) !!}
    <table id="myTable" class ="table table-striped">
        <thead>
            <td><h4>Student Name</h4></td>
            <td><h4>Student Rank</h4></td>
        </thead>

        <tbody>

        @foreach($applications as $application)
        <tr>
            <td><h5>{{ $application->Student_Name }}</h5></td>
            <td>
                {{ Form::select('ranking[' . $application->S_ID . ']', $ranks,  ['class' => 'form-control', 'placeholder' => $application->S_Rank]) }}
            </td>
        </tr>
        @endforeach

        </tbody>
    </table>

    {{ Form::Submit('Save New Ranking', ['class' => 'btn btn-primary']) }}
{!! Form::close() !!}

Controller

public function updateRank(Request $request)
{
    foreach ($request->input('rankings') as $applicationId => $rankingId) {
        Application::where('S_ID' $applicationId)->update(['ranking' => $rankingId]);
    }
}

Upvotes: 1

Sethu
Sethu

Reputation: 1379

In your code first you need to open the form before foreach and only rows of the table should be inside the loop

and for the button you have a new <tr><td></td></tr> after @endforeach and you can put the button inside of it.

I hope this will help you to fix the button

{!! Form::open(['action' => 'AbstractsController@UpdateRank' , 'method' => 'post' ]) !!}
<table id="myTable" class ="table table-striped">

    <thead>
        <td><h4>Student Name</h4></td>
        <td><h4>Student Rank</h4></td>
    </thead>

    <tbody>
        @foreach($applications as $application)
        <tr>
            <td><h5>{{$application->Student_Name}}</h5></td>
            <td><h5>
            {{Form::select('Ranking' ,$ranks,  ['class' => 'form-control', 'placeholder' =>$application->S_Rank] )}} 
            {{Form::hidden('Application_ids[]',  $application->S_ID)}}
            </h5></td>
        </tr>
        @endforeach
        <tr>
            <td>{{Form::Submit('Save New Ranking',['class' => 'btn btn-primary'])}}</td>
        </tr>
    </tbody>
</table>
{!! Form::close() !!}

Upvotes: 1

Related Questions