[英]Lumen: How to delete a record from DB?
Using Lumen, I'm displaying "Tickets". 使用流明,我正在显示“票”。 Here's my
Tickets.blade.php
: 这是我的
Tickets.blade.php
:
@foreach($tickets as $ticket)
<div>
<p>{{ $ticket->id }}</p>
<p>{{ $ticket->content }}</p>
<button onclick="deleteTicket({{$ticket->id}})">Delete</button>
</div>
@endforeach
So essentially, each time someone clicks delete, it triggers this script: 因此,基本上,每次有人单击删除时,都会触发此脚本:
<script>
function deleteTicket(id) {
jQuery.ajax({
type: "post",
url: "/tickets/deleteTicket",
data: id,
dataType: "text",
success: function () {
console.log('deleted!');
}
});
}
</script>
In my routes.php
, I have this: 在我的
routes.php
,我有这个:
$app->post('tickets/deleteTicket','TicketsController@deleteTicket');
Finally, in my TicketsController.php
, I have: 最后,在我的
TicketsController.php
,我有:
public function deleteTicket($id) {
$ticket = Ticket::find($id);
$ticket->delete();
return redirect('/tickets');
}
The problem I'm getting when I press the button (console): 当我按下按钮(控制台)时遇到的问题是:
POST http://example.com/tickets/deleteTicket 404 (Not Found)
POST http://example.com/tickets/deleteTicket 404(未找到)
I don't understand what I'm doing wrong, and why that method isn't being found. 我不明白自己在做什么错,为什么不找到这种方法。 Can anyone help out?
有人可以帮忙吗?
EDIT- I have changed my routes.php
to: 编辑-我将
routes.php
更改为:
$app->get('tickets/deleteTicket/{id}','TicketsController@deleteTicket');
My script looks the same but I changed the "type" to get
rather than post
. 我的脚本看起来相同,但是我将“ type”更改为
get
而不是post
。
If I visit, this site: http://mysite/tickets/deleteTicket/1
, the ticket will be deleted, and it redirects to tickets
page. 如果我访问此站点,请访问:
http://mysite/tickets/deleteTicket/1
,该票证将被删除,并重定向到tickets
页面。 But if the button is clicked, this error happens: 但是,如果单击该按钮,则会发生此错误:
http://MYSITE/tickets/deleteTicket?id=3 404 (Not Found)
http:// MYSITE / tickets / deleteTicket?id = 3404 (未找到)
At this point, I'm thinking that I just need to revise my AJAX call to the correct URL, but I want to make sure security is an issue. 此时,我认为我只需要将AJAX调用修改为正确的URL,但是我想确保安全性是一个问题。
Or you can try this : 或者您可以尝试以下方法:
routes.php routes.php
$app->get('tickets/deleteTicket/{id}','TicketsController@deleteTicket');
Okay here's your blade : add class to button and remove the function on click. 好的,这是您的刀片:向按钮添加类并在单击时删除功能。 like this one :
像这个 :
<button class="delete" data-id="{{$ticket->id}}>Delete</button>
And your script : 和你的脚本:
$(".delete").on("click", function(e) {
e.preventDefault();
var id = $(this).data('id');
$.ajax({
url : "tickets/deleteTicket/"+id+"",
type : "get" or "post",
success: function(data) {
alert(data);
}
});
});
Too things you must to change 太多的事情你必须改变
- In JS -在JS中
url: "/tickets/deleteTicket", // Try change to your full URL with http://
data: id, // Change to data: "id="+id,
- In Controller -在控制器中
public function deleteTicket(Request $request) {
$ticket = Ticket::find($request->id);
$ticket->delete();
return redirect('/tickets');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.