我正在关注有关Laravel的教程。
但是,我想将刀片模板Form::open()
转换为html / php形式,以使其更易于阅读和理解。
这是Blade模板:
{{ Form::open(['action'=> ['StudentController@destroy', $student->id], 'method'=>'POST']) }}
{{ method_field('DELETE') }}
{{ Form::submit('Delete',['class'=>'btn btn-danger']) }}
{{ Form::close() }}
我需要将刀片代码转换为html / php 我尝试了多次,就像这样。但失败。
<form action="url('StudentController@destroy', $student->id)" method="POST">
<?php method_field('Delete'); ?>
<button class="btn btn-danger" type="submit">Delete</button>
</form>
有人知道正确的html / php格式吗?
答案 0 :(得分:1)
您应该使用此代码
<form action="{{ url('StudentController@destroy', $student->id) }}" method="POST">
<input type='_method' value='DELETE' />
<button class="btn btn-danger" type="submit">Delete</button>
</form>
答案 1 :(得分:1)
在表单的“操作”中,您需要将所有辅助功能括在方括号中,以便Blade知道如何处理,否则,只是文本。
还要注意,我删除了'method_field'并将其替换为隐藏字段,因为这实际上是method_field
助手创建的。
<form action="{{route('StudentController@destroy', ['id' => $student->id])}}" method="POST">
<input type='hidden' value='DELETE'>
<button class="btn btn-danger" type="submit">Delete</button>
</form>
如果无法使用route
帮助程序,则可以对表单标签的“操作”参数使用更简单的方法:
<form action="/student/destroy/{{$student->id}}" method="POST">
答案 2 :(得分:1)
尝试这种方式
使用{{}}
并使用route
<form action="{{route('StudentController@destroy', ['id'=>$student->id])}}" method="POST">
<?php method_field('Delete'); ?>
<button class="btn btn-danger" type="submit">Delete</button>
</form>
答案 3 :(得分:1)
要调用控制器动作,您需要简短使用url()->action(...)
(或action()
)。
<form action="{{url()->action('StudentController@destroy', ['id'=>$student->id])}}" method="POST">
@csrf
{{ method_field('DELETE'); }}
<button class="btn btn-danger" type="submit">Delete</button>
</form>
中也有描述