我将使用 HTML 链接破坏我的用户,但它似乎没有生成正确的链接,我做错了什么?
public function destroy($id)
{
//Slet brugeren
$e = new User($id);
$e->destroy();
//Log også brugeren ud
Auth::logout();
//redrect til forsiden
Redirect::to("users/create");
}
在我看来我称之为
{{URL::action('UserController@destroy', array($user->id))}}
最佳答案
2017 年 8 月 21 日更新 Laravel 5.x
这个问题是关于 Laravel 4 的,但我把它包括在内,以防寻找 Laravel 5.x 答案的人在这里结束。表单助手(和其他一些)aren't available as of 5.x .如果您正在执行 GET 或 POST 之外的操作,您仍然需要在表单上指定一个方法。这是目前的实现方式:
<form action="/foo/bar" method="POST">
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<!-- other inputs... -->
</form>
您也可以使用 {{ method_field('PUT') }}
而不是写出隐藏的 _method
输入。
见 https://laravel.com/docs/5.4/routing#form-method-spoofing
Laravel 4 的原始答案
我认为当您单击链接时,它可能正在向该端点发送 GET 请求。 Laravel 中的 CRUD 根据 REST 工作。这意味着它期待的是 DELETE 请求而不是 GET。
这是 tutorial 中的一种可能性鲍里斯·斯特拉希亚(Boris Strahija)。
{{ Form::open(array('route' => array('admin.pages.destroy', $page->id), 'method' => 'delete')) }}
<button type="submit" class="btn btn-danger btn-mini">Delete</button>
{{ Form::close() }}
这样,您可以使用 DELETE 方法以表单形式发送请求。这篇文章解释了为什么传统链接不起作用:
You may notice that the delete button is inside a form. The reason for this is that the destroy() method from our controller needs a DELETE request, and this can be done in this way. If the button was a simple link, the request would be sent via the GET method, and we wouldn’t call the destroy() method.
关于php - CRUD Laravel 4如何链接销毁?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19643483/