提交具有多种可能动作的表单Laravel

时间:2019-06-13 07:09:39

标签: laravel forms

我正在使用Laravel框架,这里有一个表单,其中显示了一个概要文件列表(使用概要文件模型)。所有这些配置文件都与单个复选框数组绑定。单击顶部的按钮,该按钮将删除选定的配置文件(此处使用的操作是ProfileController @ deleteProfile)。此外,每个配置文件旁边都有一个按钮,单击该按钮后应该转到ProfileController @ editProfile操作,但是我不确定在表单中的何处指定此不同的操作。单击“编辑”按钮时,是否可以触发其他操作?

@if(count($profiles) > 0) 

{!! Form::open(['action' => ['AdminController@deleteProfile'], 'method' => 'POST']) !!}
{{  Form::submit('Delete Selected Profiles', ['class' => 'btn btn-danger']) }}  
<table class="table">
    <thead class="thead-dark">
        <tr>
            <th scope="col">#</th>
            <th scope="col">Select</th>
            <th scope="col">Title</th>
            <th scope="col"></th>
        </tr>
    </thead>
    <tbody>
        @foreach($profiles as $profile)
            <tr>
                <th scope="row">{{ $profile->id }}</th>

                <td>   
                    <input type="checkbox" name="selectedProfiles[]" value="{{ $profile->id }}" />                           
                </td>
                <td>{{ $profile->title }}</td>
                <td>
                    {{  Form::hidden('id', $profile->id) }}    
                    {{  Form::submit('Edit', ['class' => 'btn btn-secondary']) }}                                  
                </td>
            </tr>
        @endforeach
    </tbody>
</table>
{!! Form::close() !!}

@endif

我尝试的另一种方法是创建一个通用动作ProfileModify,在该动作中我将传递一个额外的变量来指定要实现的动作类型,即“编辑”或“删除”,但我找不到一种方法来传递当我们单击两个单独的按钮时,有条件地隐藏输入以发送“编辑”或“删除”。

有人可以建议如何解决此问题,如果这确实可行,或者如果没有,则是解决此情况的更好的替代方案?

1 个答案:

答案 0 :(得分:1)

您不需要提交表单即可链接到配置文件的编辑页面,只需使用a标签即可。

如果您已命名路线:

{{ link_to_route('profile.edit', 'Edit', ['id' => $profile->id]) }}

操作:

{{ link_to_action('ProfileController@editProfile', 'Edit', ['id' => $profile->id]) }}


Docs

相关问题