danyal

Where clause on nested relationships in eloquent

Posted 4 weeks ago by danyal

Hi, I am working on a project where we are saving user's activity and his privacy settings. I want to get user's activity based on his privacy setting. So if user has set his gpa to private then my query should not get user's activity related to gpa. My current query looks like this:

UserActivity::with(['User' => function ($q) {
                $q->with(['avatar' => function ($q2) {
                    $q2->select(['id', 'image']);
                }])
                ->select(['id', 'first_name', 'last_name', 'profile_picture']);
            }, 'User.PrivacySetting'])
            ->whereHas('User.PrivacySetting', function($q3) {
                $q3->where('gpa', '!=', PrivacySetting::PRIVACY_STATUS_NO_ONE)
                ->orWhere('cv', '=', PrivacySetting::PRIVACY_STATUS_NO_ONE)
                ->orWhere('groups', '!=', PrivacySetting::PRIVACY_STATUS_NO_ONE)
                ->orWhere('phone', '!=', PrivacySetting::PRIVACY_STATUS_NO_ONE)
                ->orWhere('courses', '!=', PrivacySetting::PRIVACY_STATUS_NO_ONE)
                ->orWhere('notes', '!=', PrivacySetting::PRIVACY_STATUS_NO_ONE)
                ->orWhere('classifieds', '!=', PrivacySetting::PRIVACY_STATUS_NO_ONE)
                ->orWhere('checkin_event', '!=', PrivacySetting::PRIVACY_STATUS_NO_ONE);
            })
            ->orderBy('id', 'desc');

This is how the schema looks: https://drive.google.com/open?id=1oYHECdby5Rco2qaj4k_MpBOnKczaNnRX

Please help me with this query! Thanks.

Please sign in or create an account to participate in this conversation.