【问题标题】:laravel delete is not workinglaravel 删除不起作用
【发布时间】:2015-08-24 01:13:40
【问题描述】:

我想知道如何将 csrf 令牌放入表单中以便删除工作?

这是我的代码:

路线:

Route::delete('category_delete/{id}',['as'=>'category_delete','uses'=>'CategoryController@destroy']);

index.blade.php

@section('adminContent')
{!! Form::token() !!}
<div class="table-responsive art-content">
    <table class="table table-hover table-striped">
        <thead>
        <th> NAME</th>
        <th> ACTIONS</th>
        </thead>
        <tbody>
        @foreach($categoriesView as $category)
            <tr>
                <td>{!! $category->name!!}</td>
                <td>{!! link_to_route('categories.edit', '', array($category->id),array('class' => 'fa fa-pencil fa-fw')) !!}</td>
                <td><button type="button" id="delete-button" class="delete-button" data-url = "{!! url('category_delete')."/".$category->id !!}"><i class="fa fa-trash-o"></i></button>
            </td>
            </tr>
        @endforeach
        </tbody>
    </table>
    <div class="pagination"> {!! $categoriesView->render() !!}</div>
</div>

@停止

类别控制器:

 public function destroy($id,Category $category)
{

    $category = $category->find ( $id );
    $category->delete ();
    Session::flash ( 'message', 'The category was successfully deleted!.' );
    Session::flash ( 'flash_type', 'alert-success' );
}

如果我使用ajax、javascript或jquery,代码应该如何?

【问题讨论】:

    标签: ajax forms laravel csrf


    【解决方案1】:

    使用 jquery 我会执行以下操作。
    在您的主页视图的标题中添加以下行
    &lt;meta name="csrf-token" content="{{ csrf_token() }}" /&gt;
    现在在你的 javascript


    function deleteMe(button)
       {
         // You might want to put the ajaxSetup function where it will always load globally.
         $.ajaxSetup({
             headers: {
                 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
             }
      });
    
       if(confirm("Are you sure?"))
       {
           $.get(
               'category_delete/'+button.id,
               function( response )
               {
                   // callback is called when the delete is done.
                   console.log('message = '+ response.message);
               }
           )
       }
      }
    

    在 index.blade.php 中,让您的 id 更具体。
    ... <td><button type="button" id=".$category->id" class="delete-button" data-url = "{!! url('category_delete')."/".$category->id !!}"><i class="fa fa-trash-o" onlick="deleteMe(this);></i></button> ...

    在您的控制器中

    public function destroy($id,Category $category){
         $category = $category->find ( $id );
         $category->delete ();
    
        return response()->json(
            'message' => 'Deleted', 
          )
    
    }
    


    Note: 不用加
    Form::token 在你看来
    希望这会有所帮助.....


    更新…………

    如果你只使用 laravel,我建议你使用 link 而不是你正在使用的 button

    在 index.blade.php 中,让您的 id 更具体。
    ... <td><a href="category_delete/".$category->id class="delete-button" data-url = "{!! url('category_delete')!!}"><i class="fa fa-trash-o"></i></td> ...
    在您的控制器中

    public function destroy($id,Category $category){
        $category = $category->find ( $id );
        $category->delete ();
    
       return view('index');//important.
    
    }
    



    如果您希望链接看起来像一个按钮,请使用 cssbootstrap 之类的 CSS 框架 应该是这样的。希望这再次有所帮助。

    【讨论】:

    • 如果我们在不使用任何javascript的情况下正常运行,那么代码应该是什么样子......
    • localhost/latestic-website/category_delete/5 错误:RouteCollection->methodNotAllowed(array('DELETE')) MethodNotAllowedHttpException
    • 在您的routes.php 中使用Route::get 而不是Route::delete
    猜你喜欢
    • 2018-10-18
    • 2014-10-19
    • 2018-12-22
    • 2015-06-03
    • 2017-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-26
    相关资源
    最近更新 更多