我正在使用Laravel 5.4
$usersWithFacebookNotifications = User::has('validSocialAccountForFacebookNotifications')
->has('activeFacebookNotifications')
->with('preferences')->with('socialAccount')
->get();
public function activeFacebookNotifications () {
return $this->notificationsChannels()
->where('active', 1)
->where('name', 'facebook_messenger');
}
public function validSocialAccountForFacebookNotifications () {
return $this->socialAccount()
->where('provider', 'facebook')
->whereNotNull('page_scoped_id');
}
是否可以在Users
模型中创建范围关系,以便我可以调用类似的东西
$users = User::has('allNeeded')->get();
其中allNeeded
为
activeFacebookNotifications + validSocialAccountForFacebookNotifications
答案 0 :(得分:2)
是的,但有点不同:
$users = User::allNeeded()->get();
在您的用户模型中添加:
public function scopeAllNeeded($query)
{
return $query
->has('validSocialAccountForFacebookNotifications')
->has('activeFacebookNotifications');
}