Laravel-更新表格中的多行

时间:2019-05-08 10:27:24

标签: php laravel

我正在尝试更新所有可用行中的一列(S_Rank)。我正在列出表格中的行,在表格内部我有下拉菜单来更改排名。我希望能够保存所有新排名。我有两个问题,第一个,提交按钮在<td> </td>之外不起作用,我不能将提交按钮放在<td>内,因为它将在每一行中列出。第二个问题是我不确定如何将所有更改保存到数据库中

当前问题是我无法使按钮在<td></td>标记之外起作用,任何人都可以帮助

在我看来

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

在我的控制器中

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

3 个答案:

答案 0 :(得分:1)

首先在代码中,您需要在foreach之前打开表单,并且循环中仅应包含表的行

对于该按钮,您在<tr><td></td></tr>之后有一个新的@endforeach,可以将按钮放在其中。

我希望这可以帮助您修复按钮

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

答案 1 :(得分:1)

免责声明:此代码未经测试,也不是解决此问题的最有效方法。这只是指出问题的一个途径。

查看

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

控制器

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

答案 2 :(得分:0)

移动

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

到循环和表之外。您只需要定义一次即可。

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