Php Laravel-包含软删除数据的隐式路由模型绑定
我有个小问题。有两个用户角色,一个是普通成员,一个是管理员。会员可以删除一个博客,他们删除(软删除)后将无法看到该博客,而管理员仍然可以看到该博客,即使它是软删除的 示例代码:Php Laravel-包含软删除数据的隐式路由模型绑定,php,laravel,routes,laravel-5.2,Php,Laravel,Routes,Laravel 5.2,我有个小问题。有两个用户角色,一个是普通成员,一个是管理员。会员可以删除一个博客,他们删除(软删除)后将无法看到该博客,而管理员仍然可以看到该博客,即使它是软删除的 示例代码: // Route file Route::get('/blog/{blog}', 'BlogController@show'); // BlogController public function show(App\Blog $blog) { // It never gets to here if the
// 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
facade,但仍然无法工作
编辑:当我转储并死亡时,它给我一个null
值Auth::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.
}
“它不让我”是什么意思?错误消息?对不起,我更新了问题。但是它只是说null,并且说不能检查null上的函数。。