Laravel-使用表单更新多行值

时间:2019-05-08 20:12:56

标签: php laravel

我正在列出一个表中的许多行,我试图使用每行中存在的下拉菜单来编辑它们。下面的代码很好,但是它将仅存储最后一个(S_Rank)值(这是我要更改的列),这是获取每一行输入的最佳方法。我知道表单不是数组的问题,将其变成数组的方式是什么

我的观点

{!! 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>

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

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

<td><h5>

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

</h5></td>

</tr>

@endforeach

</tbody>
</table>

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

我的控制器

foreach(request('Student_ids') as $S_ID){
            $application = Application::where('S_ID' , $S_ID)->get();
            $application->S_Rank =  $request-> input('Ranking');
            $application->save();}

我正在尝试使用每个下拉菜单中输入的值更新每一行

2 个答案:

答案 0 :(得分:0)

我想你已经回答了。我认为您需要将$ranks变量更改为$ranks[],因为您实际上只是采用他们在选择中选择的最后一个值,因此,您将等级存储在一个维度数组中,应该与您的Student_ids []隐藏数组相对应

答案 1 :(得分:0)

如果要更新每个应用程序的排名,则需要知道哪个排名值分配给哪个应用程序。假设S_ID是应用程序的唯一标识符,则可以在视图中执行以下操作:

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

然后在您的控制器中:

foreach(request('Ranking') as $S_ID => $rank){
        $application = Application::where('S_ID' , $S_ID)->first();
        $application->S_Rank = $rank;
        $application->save();}
相关问题