我有一个管理面板,其中有3种类型的用户:管理员,教师和学生。现在,我希望当学生登录时,他/她只看到由管理员上传的数据
根据他/她的班级和班级分组,例如 class = 10th 和 group = computer science 。我不知道我怎么能得到这种东西。我使用了以下代码
$paper = Paper::where('paper_type','PaperSolution' && 'class',Auth::user()->class)->get();
由于我正在转储数据,因此无法正常工作
dd($paper);
它给我的答案是空的。
答案 0 :(得分:1)
请参考Laravel集合文档以更正语法。
$paper = Paper::where('paper_type','PaperSolution')
->where('class', Auth::user()->class)
->get();
答案 1 :(得分:1)
您可以使用更细粒度的位置作为数组传递:
$query->where([
['column_1', '=', 'value_1'],
['column_2', '<>', 'value_2'],
[COLUMN, OPERATOR, VALUE],
...
])
尝试一下
$paper = Paper::where([['paper_type','PaperSolution'],['class', Auth::user()->class]])->get();
dd($paper);
OR
$paper = Paper::where([['paper_type','=','PaperSolution'],['class','=', Auth::user()->class]])->get();
dd($paper);
使用数组查询位置的参考
答案 2 :(得分:0)
我不完全了解您的数据库的结构,但是首先,您需要具有相应的列才能编写任何查询。您的第一个选项是uploaded by admin
,因此在您的上载表格(我可以从您的文本中看到的是Paper模型)中,应该有类似uploader_role
的名称。然后,您有student_class
和student_group
选项。我实际上想引起您的注意,这2个选项是users
表列而不是uploads
(纸)。因此,您需要检查用户的某些权限,然后让管理员上传论文。您可以使用中间件或仅在控制器中完成
$user = Auth::user();
if ($user->class == 10 && $user->group == 'computer_science') {
$papers = Paper::where('uploader_role', 'admin')
// you can add extra where options if that columns exist in papers table and you need to filter them also by class and group
->where( 'class_that_has_permissions', $user->class)
->where( 'group_that_has_permissions', $user->group)
->get();
return $papers;
}
abort(404);
还要注意名称为class
的列,这些列可以返回真实的用户类名称,例如App\User
,并尝试使用class_name
或class_number
。