Laravel 4 restful 使用资源控制器删除记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19167762/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:30:43  来源:igfitidea点击:

Laravel 4 restful delete a record with a resource controller

laravellaravel-4

提问by Christopher Kikoti

I am new to Laravel framework but I really like it. My biggest problem is I have been searching on how I can delete a single record using resource controller.

我是 Laravel 框架的新手,但我真的很喜欢它。我最大的问题是我一直在寻找如何使用资源控制器删除单个记录。

Controller method:

控制器方法:

public function destroy($id) {
    $department = Department::find($id);
    $department->delete();
}

Delete link I have tried:

删除我尝试过的链接:

<a class="btn btn-xs btn-danger" data-method="delete" href="{{ URL::to('department/' . $department->id) }}"><i class="icon-remove"></i></a>

Javascript:

Javascript:

<script type="text/javascript">
$(function(){
    $('[data-method]').append(function(){
        return "\n" +
        "form action='" + $(this).attr('href') + "' method='post' style='display: none;'>\n" + 
        "<input type='hidden' name='_method' value='"+$(this).attr('data-method')+"'>\n" +
        "</form>\n"
    })
    .removeAttr('href')
    .attr('style', 'cursor: pointer;')
    .attr('onclick', '$(this).find("form").submit();');
});

Now when I click on the delete link, it doesn't work. Any idea on what am I doing wrong, I have been searching for so long.

现在当我点击删除链接时,它不起作用。关于我做错了什么的任何想法,我一直在寻找这么长时间。

回答by nietonfir

Use $.post()to trigger your click instead of that form creation/submission:

使用$.post()触发您的点击而不是该表单创建/提交:

$(document).on("click", "[data-method]", function(e) {
    e.preventDefault();

    $.post($(this).attr('href'), {/* the id goes here */});
});

Apply the cursor style via CSS. I must admit that I'm not sure if laravel expects a HTTP DELETE instead of a post. And I think you missed to submit the id of the department you want to delete.

通过 CSS 应用光标样式。我必须承认,我不确定 laravel 是否需要 HTTP DELETE 而不是 post。而且我认为您错过了提交要删除的部门的 ID。

[edit] As laravel expects a HTTP DELETE you can't use the $.post() shorthand, but $.ajax()instead:

[编辑] 因为 laravel 需要一个 HTTP DELETE 你不能使用 $.post() 简写,而是使用$.ajax()代替:

$(document).on("click", "[data-method]", function(e) {
    e.preventDefault();

    $.ajax({
        url: $(this).attr('href'),
        type: "DELETE",
        data: {/* the id goes here */},
        success: function(data, textStatus, jqXHR) {
             console.log("success");
        }
    });
});

回答by devo

The destroy()will be called in DELETErequest and not in POSTrequest.

destroy()将被调用的DELETE请求,而不是POST请求。

So try ,

所以尝试,

<a class="btn btn-xs btn-danger" onclick="deleteDepartment($department->id)" href="javascript:void(0)"><i class="icon-remove"></i></a>

And in javascript,

在 JavaScript 中,

function deleteDepartment(id) {
  $.ajax({
    url: 'department/'+id,
    type: 'DELETE',
    success: function(result) {
        // Do something with the result
    }
  });
}

回答by Christopher Kikoti

Thanks for the answers guys, it works just as it is I just forgot the opening < when creating a form ie I was mistakenly wrote:

感谢您的回答,它的工作原理就像我在创建表单时忘记了开头 < 即我错误地写道:

"form action='" + $(this).attr('href') + "' method='post' style='display: none;'>\n" + 

Instead of:

代替:

"<form action='" + $(this).attr('href') + "' method='post' style='display: none;'>\n" +