auth立面无法在没有圣所API的情况下用作Laravel 8的中间件
我正在创建一个API,任何人都可以查看页面,但是只有管理员才能看到所有帖子,而用户仅限于批准。这是通过iS_verified
布尔值变量实现的,在其中给admin的值为1,用户用户值为0。我想创建一个这样的函数,
public function show(){
if(Auth::check()){
$showAllDetails = Events::all();
echo $showAllDetails;
}else {
$showUserDetails = Events:all()->where('is_verified',1);
echo $showUserDetails;
}
}
但是auth:check
仅在 如果我在HoppsCotch上
Route::middleware('auth:sanctum')->group(function () {
Route::get('view', [ViewController::class, 'show']);
});
运行此代码,则只能显示何时登录(用户不需要登录)。因此,用户看不到任何帖子。如果我删除auth:Sanctum
中间件,则只能运行代码的其他部分,并且没有验证检查或任何东西可以运行。
我需要一种将两者都合并到单个功能中的方法,以便我可以创建一个单个路线,而不是为不同人创建两个路线。有什么办法做这样的事情吗?
I'm creating an api through which anybody can view a page, however only admin can see all posts, while users are restricted to approved only. This is implemented via is_verified
boolean variable where admin is given value of 1 and user the value of 0. I want to create a function like this
public function show(){
if(Auth::check()){
$showAllDetails = Events::all();
echo $showAllDetails;
}else {
$showUserDetails = Events:all()->where('is_verified',1);
echo $showUserDetails;
}
}
However Auth:check
only works if I have sanctum api in my route
Route::middleware('auth:sanctum')->group(function () {
Route::get('view', [ViewController::class, 'show']);
});
If I run this code on Hoppscotch, it only shows if the admin is logged in (User don't require login). So a user can't see any post. If I remove the auth:sanctum
middleware, only the else part of the code runs and no auth check or any stuff can run .
I need a way to incorporate both in a single function so that I can create a single route instead of creating two routes for different persons. Any way of doing such things?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想您的其他部分是错误的查询,更改您的其他部分
I guess your else part is incorrect query, change your else part like above