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

我查了下,视图文件里的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);
    }
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
最佳答案

@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

请贴出相关代码

6年前 评论

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

6年前 评论

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

6年前 评论

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

6年前 评论

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

6年前 评论

@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年前 评论

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

5年前 评论

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

4年前 评论

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