返回总是正确的

发布于 2025-02-13 21:26:31 字数 1154 浏览 0 评论 0 原文

我想隐藏一个只为用户显示“ admin”或“ writer”的用户的div,不需要两者,使用一个变量 $ check ,如果用户有这些角色之一,所以我尝试使用视图控制器中的以下逻辑来进行操作:

 public function index(){
    $check = false;
    if(auth()->user() != null){
        $check = auth()->user()->roles()
                    ->where('name', 'admin')
                    ->orWhere('name','writer')
                    ->exists();
        dd(auth()->user()->roles()->Where('name','writer')->exists());
        // dd($check);
    }
    return view('albums.index', [
            'albums'    => Album::latest()->filter(request(['tag','search','label','date']))->paginate(6),
            'check'     => $check,
        ]);
}

问题是变量 $ check 始终返回每个用户,我删除了or Where()part查询的内容,它变成了错误我认为,尽管没有在角色用户模块的枢轴表上显示,但我认为,默认情况下,用户可能会以某种方式扮演“作者”角色,所以我做了一个快速的转储,并确保了一个快速的转储返回的false

“

我确保 or Where()是什么使用其他DDS引起这一点, 我检查了文档,我认为我有查询的语法对吗?

I want to hide a div that only shows for the users with the role "admin" or "writer", doesn't need to be both, using a variable $check that becomes true if the user has one of these roles, so i tried to do it using the following logic in the view controller :

 public function index(){
    $check = false;
    if(auth()->user() != null){
        $check = auth()->user()->roles()
                    ->where('name', 'admin')
                    ->orWhere('name','writer')
                    ->exists();
        dd(auth()->user()->roles()->Where('name','writer')->exists());
        // dd($check);
    }
    return view('albums.index', [
            'albums'    => Album::latest()->filter(request(['tag','search','label','date']))->paginate(6),
            'check'     => $check,
        ]);
}

the problem is that the variable $check always returns true for every single user, I removed the orWhere() part of the query and it became turned back to false the way it was supposed to be, I thought maybe somehow the users were getting the 'writer' role by default despite it not showing on the pivot table of the Role User modules, so i did a quick dump and die to be sure and it returned false

dump and die with role 'writer'

I made sure that OrWhere() is what's causing this using other DDs,
I checked the documentation and I think I got the syntax of the query right?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仄言 2025-02-20 21:26:31

请尝试使用此查询:

 public function index(){
    $check = false;
    if(auth()->user() != null){
        $check = auth()->user()->roles()
        ->where(function($query){
            $query->where('name','admin');
            $query->where('name', '!=','writer');
        })
        ->orWhere(function($query){
            $query->where('name', '!=','admin');
            $query->where('name','writer');
        })->exists();
        dd($check);
    }
    return view('albums.index', [
            'albums'    => Album::latest()->filter(request(['tag','search','label','date']))->paginate(6),
            'check'     => $check,
        ]);
}

Please try to use this query:

 public function index(){
    $check = false;
    if(auth()->user() != null){
        $check = auth()->user()->roles()
        ->where(function($query){
            $query->where('name','admin');
            $query->where('name', '!=','writer');
        })
        ->orWhere(function($query){
            $query->where('name', '!=','admin');
            $query->where('name','writer');
        })->exists();
        dd($check);
    }
    return view('albums.index', [
            'albums'    => Album::latest()->filter(request(['tag','search','label','date']))->paginate(6),
            'check'     => $check,
        ]);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文