到这里编辑功能是好用的,但是到了删除功能,就不能删除,大家知道原因吗?
我查了下,视图文件里的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);
}
推荐文章: