Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php larvel使用get()和特定角色_Php_Laravel - Fatal编程技术网

Php larvel使用get()和特定角色

Php larvel使用get()和特定角色,php,laravel,Php,Laravel,6个项目 还有用户模型,这是用户模型中的一种方法 public function setPasswordAttribute($password) { $this->attributes['password'] = bcrypt($password); } 当我像这样在laravel中使用get()时,一切都正常工作 User::get(); public function setRoleToGetData() { $user = Auth::user()->getS

6个项目 还有用户模型,这是用户模型中的一种方法

public function setPasswordAttribute($password)
{
    $this->attributes['password'] = bcrypt($password);
}
当我像这样在laravel中使用get()时,一切都正常工作

User::get();
public function setRoleToGetData()
{
  $user  = Auth::user()->getSex // getSex is method in the model 
  if($user == 1)
    return users in the whole program like this 
    User::where('user_sex','=',1)->get();
  else
    // return the reverse 
}
它返回所有用户。 我的问题是,是否有一种方法可以在模型中编写方法,该方法可以返回具有条件的用户,而不是所有用户, 像这样

User::get();
public function setRoleToGetData()
{
  $user  = Auth::user()->getSex // getSex is method in the model 
  if($user == 1)
    return users in the whole program like this 
    User::where('user_sex','=',1)->get();
  else
    // return the reverse 
}
我不想每次我想获得用户时都写
where('user_sex','=',1)

谢谢

您可能正在寻找示波器

从文档中:

局部作用域允许您定义公共约束集,这些约束集可以在整个应用程序中轻松重复使用

例如:

public function scopePopular($query)
{
    return $query->where('votes', '>', 100);
}
然后用作

SomeModel::popular()->get();

您可能正在寻找范围

从文档中:

局部作用域允许您定义公共约束集,这些约束集可以在整个应用程序中轻松重复使用

例如:

public function scopePopular($query)
{
    return $query->where('votes', '>', 100);
}
然后用作

SomeModel::popular()->get();

有此的全局作用域有此的全局作用域