1

I'm trying to retrieve Player with user and calendars - Player hasOne user and Player hasMany calendars. But I need only male users - works fine, then I have to check calendars collision - select only players without calendar collision, query:

$freeplayers = Player::whereNotIn('id', $playersev)
                         ->with('user','calendars')
                         ->whereHas('user', function ($query) {
                            $query->where('gender', 'male');
                         })
                         ->whereHas('calendars', function ($query) use ($event) {
                             $query->whereNotIn('fulldate', $event->fulldate);
                         })
                         ->get();

Second whereHas is not working because:

  • not every player has an event in calendar (so I cannot access to fulldate attr)
  • player hasMany calendars, so it is returned as another collection, I have to run ->whereNotIn(... in every item of this calendars collection...

So, I need male players with empty calendar (they are available) and players without collision in their existing calendar.

How can I achieve that? I tried to combine ->doesntHave('calendars')->orWhereHas('cale... but it's not working because if user have calendar, it is returned as another collection...

Thanks

2
  • can you show what fullDate is? Commented Nov 7, 2017 at 17:35
  • @NiRR - just ordinary datetime, for example 2017-11-10 16:00:00 Commented Nov 7, 2017 at 17:47

2 Answers 2

1

for complete API of the builder, use https://laravel.com/api/5.2/Illuminate/Database/Eloquent/Builder.html

you need to do a "sub query" in the sense that you want ONE of the following to occur: either the calendar is empty or the calendar date is not a specific date.

so

Player::where(function($query) use ($forbiddenDatesArray) {
    $query->has('calendars', 0)
          ->orWhereHas('calendars', function($query) use ($forbiddenDatesArray){
              $query->whereNotIn('fullDate', $forbiddenDatesArray);
          });
});
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! It works, thank you, just small typo - calendars not calendar :)
1

whereNotIn checks the value to the given array as second argument

Try

->whereHas('calendars', function ($query) use ($event) {
    $query->where('fulldate', '!=', $event->fulldate);
})

Update: To get player of no calendar entry

->whereHas('calendars', function ($query) use ($event) {
    $query->where('fulldate', '!=', $event->fulldate)
          ->orWhere('fulldate', null);
})

7 Comments

Thanks, now that date check works, but players without calendars are not included
Great! Happy to help. Accept the answer if it helped.
Well I need players without calendar to be included too :)
Updated answer for players without calendar.
This will return only players WITH calendar, if player doesn't have calendar, there is no fulldate property, whole calendar object is empty. NiRR's solution works
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.