Php larvel使用get()和特定角色
6个项目 还有用户模型,这是用户模型中的一种方法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
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();
有此的全局作用域有此的全局作用域