Laravel - 删除按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41203248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel - delete button
提问by tomczas
Welcome ! I'm new to laravel and i have problem. I start my private project and i want to delete query from database using delete button ( simple todo app). When i click it it doesn't delete but only redirect me to id of this query. Dont know what to do. Can someone help me?
欢迎 !我是 Laravel 的新手,我遇到了问题。我开始我的私人项目,我想使用删除按钮(简单的待办事项应用程序)从数据库中删除查询。当我单击它时,它不会删除,而只会将我重定向到此查询的 ID。不知道该怎么办。有人能帮我吗?
Regards tomczas
问候汤姆查斯
Destroy function in homecontroller
家庭控制器中的销毁功能
public function destroy($id)
{
$todo = Todo::findOrFail($id);
$todo->delete();
return back();
}
home blade
家用刀片
@foreach($todos as $todo)
<ul class="todo-list">
<li>
<!-- drag handle -->
<span class="handle">
<i class="fa fa-ellipsis-v"></i>
<i class="fa fa-ellipsis-v"></i>
</span>
<!-- checkbox -->
<input type="checkbox" value="">
<!-- todo text -->
<span class="text">{{$todo->tytul}}</span>
<!-- Emphasis label -->
<small class="label label-danger"><i class="fa fa-clock-o"></i> {{$todo->czas}}</small>
<!-- General tools such as edit or delete-->
<div class="tools">
<i class="fa fa-edit"></i>
{{Form::open([ 'method' => 'DELETE', 'route' => [ 'home.destroy', $todo ] ])}}
{{ Form::hidden('id', $todo->id) }}
{{Form::button('<i class="fa fa-trash-o"></i>', array('type' => 'submit', 'class' => ''))}}
{{ Form::close() }}
<i class="fa fa-trash-o"></i>
</div>
</li>
</ul>
@endforeach
回答by num8er
Fix the form:
修正表格:
{{Form::open(['method' => 'DELETE', 'route' => ['home.destroy', $todo->id]])}}
and no need for:
并且不需要:
{{ Form::hidden('id', $todo->id) }}
... laravel does all magick
... laravel 可以做所有魔法
p.s. make sure You've created resource route to make home controller act as REST controller.
extra, watch this video: https://www.youtube.com/watch?v=6pjPXOwKzJM
another way of doing delete function is that -
You can create some route:
ps 确保您已创建资源路由以使家庭控制器充当 REST 控制器。
另外,观看此视频:https: //www.youtube.com/watch?v=6pjPXOwKzJM
另一种执行删除功能的方法是 - 您可以创建一些路线:
Route::get('home/{id}/delete', ['uses' => 'HomeController@destroy', 'as' => 'home.delete']);
and in this case no need for form:
在这种情况下不需要表格:
<div class="tools">
<i class="fa fa-edit"></i>
<a href="{{route('home.delete', $todo->id)}}?{{time()}}">
<i class="fa fa-trash-o"></i>
</a>
</div>
p.s. after reading Your routes I see also core problem:
ps 阅读您的路线后,我还看到了核心问题:
Route::resource('home', 'HomeController@index');
should be (read this):
应该是(阅读本文):
Route::resource('home', 'HomeController');
回答by tomczas
Ok second idea is working great but little mistake in spelling:
好的,第二个想法很有效,但拼写错误很少:
should be:
应该:
Route::get('/home/{id}/delete', ['uses' => 'HomeController@destroy', 'as' => 'home.delete']);
so {id}
not :id
and uses
not use
Thank u very much for help !
所以{id}
不:id
和uses
不use
感谢ü非常多的帮助!
回答by hamidreza ghanbari
in a new version of laravel 5.6 , laravel suggest to use code below to delete one row
在新版本的laravel 5.6 中,laravel 建议使用下面的代码删除一行
<form method="post" action={{ route('yourControllerDeleteMethodName'), ['yourRowName'=> $yourRowName->slug] }}>
<input type="hidden" value="DELETE"/>
<button type="submit">click to delete</button>
</form>
<form method="post" action={{ route('yourControllerDeleteMethodName'), ['yourRowName'=> $yourRowName->slug] }}>
<input type="hidden" value="DELETE"/>
<button type="submit">click to delete</button>
</form>
this work when you use slug to get row if you use id to get row just write $id instead of slug.
当您使用 slug 获取行时,如果您使用 id 获取行,则此工作仅写入 $id 而不是 slug。
hope to work for you:)\
希望为你工作:)\