CRUD Laravel 4如何链接销毁? [英] CRUD Laravel 4 how to link to destroy?

查看:58
本文介绍了CRUD Laravel 4如何链接销毁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将使用HTML链接销毁用户,但是它似乎无法生成正确的链接,我在做什么错了?

I will destroy my user with a HTML link, but it doesn't seem to generate the correct link, what am i doing wrong?

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

In my view i call this {{URL::action('UserController@destroy', array($user->id))}}

推荐答案

Laravel 5.x更新2017年8月21日

该问题询问有关Laravel 4的问题,但如果有人在寻找Laravel 5.x答案的地方落在这里,则我将其包括在内.表单助手(和其他一些助手) aren' t自5.x版起可用.如果您要执行除GET或POST之外的其他操作,则仍需要在表单上指定方法.这是目前实现这一目标的方法:

The question asks about Laravel 4, but I include this in case people looking for Laravel 5.x answers end up here. The Form helper (and some others) aren't available as of 5.x. You still need to specify a method on a form if you are doing something besides GET or POST. This is the current way to accomplish that:

<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输入.

You can also use {{ method_field('PUT') }} instead of writing out the hidden _method input.

请参见 https://laravel.com/docs/5.4/routing#形式方法欺骗

Laravel 4的原始答案

我认为,当您单击链接时,它可能正在向该端点发送GET请求. Laravel中的CRUD根据REST进行工作.这意味着它期望一个DELETE请求而不是GET.

I think when you click the link, it is probably sending a GET request to that end point. CRUD in Laravel works according to REST. This means it is expecting a DELETE request instead of GET.

这是Boris Strahija的教程中的一种可能性. /p>

Here's one possibility from a tutorial by 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方法的表单发送请求.这篇文章解释了为什么传统链接不起作用:

This way, you send the request in a form with the DELETE method. The article explains why a traditional link won't work:

您可能会注意到,删除按钮位于表单内部.原因是我们控制器的destroy()方法需要一个DELETE请求,并且可以通过这种方式完成.如果按钮是简单链接,则请求将通过GET方法发送,而我们不会调用destroy()方法.

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.

这篇关于CRUD Laravel 4如何链接销毁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆