简体   繁体   English

如何使用JQuery和Laravel从数据库中删除

[英]How to delete from the database using JQuery and Laravel

I have the following code with an if statement depending if a user has saved an article or not. 我有以下代码和if语句,具体取决于用户是否保存了文章。 I'm simply trying to delete the article from the database using jquery. 我只是试图使用jquery从数据库中删除文章。 I unsure where im going wrong? 我不确定我哪里出错了? help is much appreciated! 非常感谢您的帮助!

View: 视图:

<form action="{{URL::route('article-delete')}}" method="post" id="article_one_delete">
<div class="form-group">
<input type="hidden" name="first_desc" value="{{$firstrow->description}}" class="form-control">
</div>
<div class="form-group">
<input type="hidden" name="first_title" value="{{$firstrow->title1}}" class="form-control">
</div>

<button type ="button" id="Recodelete" class="btn btn-success btn-xs">UnSave</button>
{{Form::token()}}

</form>

Route: 路线:

Route::delete('/home/', array( 'as' => 'article-delete', 
'uses' => 'HomeController@deletearticle'));

Controller: 控制器:

public function deletearticle(){

 $firsttitle       = Input::get('first_title');

$articledelete  = UserSaveArticle::where('user_id', Auth::id()
 ->where ('user_save_articles.chosen_title', $firsttitle))->delete();

return true;

JQuery: JQuery的:

$(document).ready(function(){
    $('#Recodelete').on('click', function(){
       var article_one_delete = $('#article_one_delete').serializeArray();
       var url_d         = $('#article_one_delete').attr('action');

       $.get(url_d, article_one_delete, function(data){
           console.log(data);
       });
    });
 });
  1. You should define right route for DELETE article, like this: 您应该为DELETE文章定义正确的路线,如下所示:
Route::delete('/article/{id}', ['as' => 'article-delete', 'uses' => 'HomeController@deleteArticle']);
  1. In the HomeController $id variable (article ID) will be available as a method parameter: HomeController $ id变量(商品ID)将作为方法参数提供:
function deleteArticle($id)
{
    …
}
  1. In PHP side you defined DELETE route, it means you should make DELETE request on JS side using the ajax method: 在PHP端,您定义了DELETE路由,这意味着您应该使用ajax方法在JS端发出DELETE请求:
$.ajax({
    url: '/article/' + articleId,
    type: 'DELETE',
    success: function(result) {
        // Do something with the result
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM