雄辩的范围关系

时间:2017-10-13 20:03:29

标签: php laravel eloquent

我正在使用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

1 个答案:

答案 0 :(得分:2)

是的,但有点不同:

$users = User::allNeeded()->get();

在您的用户模型中添加:

public function scopeAllNeeded($query)
{
    return $query
        ->has('validSocialAccountForFacebookNotifications')
        ->has('activeFacebookNotifications');
}