到这里编辑功能是好用的,但是到了删除功能,就不能删除,大家知道原因吗?

我查了下,视图文件里的URL是对的。
TOPICSCONTROLLER里面的destroy method是对的,但愣是删不了。点了下,还是返回到原页面。
大家知道原因吗?
larabbs\app\Http\controllers\topicscontroller.php

public function destroy(Topic $topic)
    {
        $this->authorize('destroy', $topic);
        $topic->delete();

        return redirect()->route('topics.index')->with('message', '话题删除成功.');
    }

larabbs\app\Models\User.php

public function isAuthorOf($model)
    {
        return $this->id == $model->user_id;
    }

larabbs\resources\views\topics\show.blade.php

@can('update', $topic)
                        <div class="operate">
                            <hr>
                            <a href="{{ route('topics.edit', $topic->id) }}" class="btn btn-default btn-xs" role="button">
                                <i class="glyphicon glyphicon-edit"></i> 编辑
                            </a>
                            <a href="{{ route('topics.destroy', $topic->id) }}" class="btn btn-default btn-xs" role="button">
                                <i class="glyphicon glyphicon-trash"></i> 删除
                            </a>
                        </div>
@endcan

路由文件

Route::resource('topics', 'TopicsController', ['only' => ['index', 'show', 'create', 'store', 'update', 'edit', 'destroy']]);

policy文件

public function update(User $user, Topic $topic)
    {
        return $user->isAuthorOf($topic);
    }

    public function destroy(User $user, Topic $topic)
    {
        return $user->isAuthorOf($topic);
    }
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
最佳答案

@sachu 请查看这部分代码。

<form action="{{ route('topics.destroy', $topic->id) }}" method="post">
        {{ csrf_field() }}
        {{ method_field('DELETE') }}
        <button type="submit" class="btn btn-default btn-xs pull-left" style="margin-left: 6px">
            <i class="glyphicon glyphicon-trash"></i>
            删除
        </button>
</form>
6年前 评论
讨论数量: 10

@sachu 请查看这部分代码。

<form action="{{ route('topics.destroy', $topic->id) }}" method="post">
        {{ csrf_field() }}
        {{ method_field('DELETE') }}
        <button type="submit" class="btn btn-default btn-xs pull-left" style="margin-left: 6px">
            <i class="glyphicon glyphicon-trash"></i>
            删除
        </button>
</form>
6年前 评论

@sachu 有兩個方法可以解決

  1. 將刪除的 hyperlink 改為 form 表單,以使用 表單方法偽造
  2. 在路由文件中,加入以下程式碼,將 delete 方法開放為 get 方法也可使用(不建議)
    Route::get('topics/{topic}/delete', 'TopicsController@destroy');
6年前 评论

请贴出相关代码

6年前 评论

请对照自己模板部分的代码,这里是伪造了一个删除的请求,不是直接 get 请求。
@sachu

6年前 评论

@wlight 麻烦你,你在哪里看出是伪造了一个get请求,多谢大神。

6年前 评论

@sachu 参考下 csrf_field() 和 method_field('DELETE')

6年前 评论

@Tablib 大哥能不能明示啊,谢谢

6年前 评论

Route::resource('topics', 'TopicsController', ['only' => ['index', 'show', 'create', 'store', 'update', 'edit', 'destroy']]);
最好等把段路由等同于哪些贴出来

5年前 评论

我也遇到这个问题所以很直接的就是没有添加表单验证等!

4年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!