简体   繁体   English

Laravel-更新表中的多行

[英]Laravel - update multiple rows in a table

I am trying to update one column (S_Rank) in all the available rows. 我正在尝试更新所有可用行中的一列(S_Rank)。 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. 我有两个问题,第一个,提交按钮在<td> </td>之外不起作用,并且我不能将提交按钮放在<td>因为那样它将在每一行中列出。 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 当前的问题是我无法使按钮在<td></td>标记之外起作用,任何人都可以帮忙

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

In your code first you need to open the form before foreach and only rows of the table should be inside the loop 首先在代码中,您需要在foreach之前打开表单,并且循环中仅应包含表的行

and for the button you have a new <tr><td></td></tr> after @endforeach and you can put the button inside of it. 对于按钮,在@endforeach之后有一个新的<tr><td></td></tr> ,您可以将按钮放在其中。

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() !!}

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

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() !!}

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

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