我有个小问题。有两个用户角色,一个是普通成员,一个是管理员。该成员可以删除一个博客,他们将无法看到博客后,他们删除(软删除),而管理员仍然可以看到博客,即使它是软删除。
示例代码:
// Route file
Route::get('/blog/{blog}', 'BlogController@show');
// BlogController
public function show(App\Blog $blog) {
// It never gets to here if the blog has been soft deleted...
// Automatically throws an 404 exception
}
我希望管理员能够访问博客,即使它是软删除,但它并不真的工作。我正在尝试编辑路由服务提供者,但我没有得到任何运气,因为它不允许我使用Auth::user()
函数来获取登录用户,这样我就可以检查他们是否有权限。
我的RouteServiceProvider
$router->bind('post', function($post) {
if (Auth::user()->isAdmin()
return Post::withTrashed()->where('id', $post)->firstOrFail();
});
这不起作用,因为它不知道Auth::user()
是什么。我已经导入了Auth
外观,但仍然无法工作。
编辑:当我转储和死掉null
时,它会给我一个Auth::user()
值。
任何帮助都是非常感谢的。
发布于 2016-04-24 18:46:40
我刚刚发现,在路由服务提供者中获取current logged in user
是不可能的,因为它会在所有会话服务提供者之前加载。
相反,我只是做了:
//Route Service Provider
$router->bind('post', function($post)
return Post::withTrashed()->where('id', $post)->firstOrFail();
});
// Controller
public function show(Post $post) {
// If the post has been trashed and the user is not admin, he cannot see it
if (!Auth::user()->isAdmin() && $post->trashed())
abort(404);
// Proceed with normal request because the post has not been deleted.
}
发布于 2022-04-19 09:32:13
当这个问题在谷歌搜索时弹出时,在更新的Laravel版本中,您可以这样做:
Route::get('posts/{post}', [PostController::class, 'show'])->withTrashed();
请参阅:隐式软删除模型
https://stackoverflow.com/questions/36824809
复制相似问题