Support the ongoing development of Laravel.io →
Eloquent Laravel
Last updated 2 years ago.
0

im trying to help, hope this help

this is an eager relation, which mean you still get the users but the relationship to the opponent is already loaded, and will return null for that relation if closure did not satisfy

$users = User::role('DJ')->with(['events' => function ($query) {
    			$query->where('start', '>', 'afterMinimumDate')->where('start','<','beforeMaximumDate');
		}])->get();

$notTiedUsers = [];
foreach($users as $user){
    if($user->events == null){
        //# here is the user who is not tied
        $notTiedUsers[] = $user;
    }
}


Last updated 7 years ago.
0

Thank you for the reply!

The one issue I am still running into is if a User doesn't have any events yet. If this is the case the User isn't even returned.

Anyway to return a user even if events on that user in null?

0

have you tried it?

users that dont have events will be in $notTiedUsers..

yes eager still return the users even the closure doesnt satisfy, but return null when trying to get the relation to the opponent

0

Ok so I tried a few things but still stuck.

Is there a way to not return the original model if the relationship where fails?

For example in this:

$users = User::role('DJ')->with(['events' => function ($query) {
                $query->where('start', '<>', '2017-10-20');
        }])->get();

If the where start <> 2017-10-20 fails meaning there is an event for then user with a start of that date then don't return that user at all?

0

try with "whereHas()"

Example:

$users = User::role('DJ')->with('events')->whereHas('events', function ($query){
  $query->where('start', '<>', '2017-10-20');
})->get();
0

Man I thought this would work but still no go. It event loads the event and user with a start equal to what I said not to be equal to.

Here is the json dump:

[
{
id: 2,
name: "Dave Kanaszka",
order: null,
events: [
{
id: 15,
title: "Default",
start: "2017-10-01",
end: null,
description: "Default"
}
]
},
{
id: 3,
name: "Kyle Kydd",
order: null,
events: [
{
id: 18,
title: "Default",
start: "2017-10-01",
end: null,
description: "Default"
},
{
id: 19,
title: "test",
start: "2017-11-01",
end: null,
description: "test"
}
]
},
{
id: 4,
name: "Rick",
order: null,
events: [
{
id: 16,
title: "Default",
start: "2017-10-01",
end: null,
description: "Default"
}
]
},
{
id: 5,
name: "Carlos",
order: null,
events: [
{
id: 10,
title: "Default",
start: "2017-10-01",
end: null,
description: "Default"
}
]
},
{
id: 6,
name: "Chris",
order: null,
events: [
{
id: 11,
title: "Default",
start: "2017-10-01",
end: null,
description: "Default"
}
]
},
{
id: 7,
name: "Steve",
order: null,
events: [
{
id: 12,
title: "Default",
start: "2017-10-01",
end: null,
description: "Default"
}
]
},
{
id: 8,
name: "Stephen",
order: null,
events: [
{
id: 13,
title: "Default",
start: "2017-10-01",
end: null,
description: "Default"
}
]
}
]

And here is the query:

$users = User::role('DJ')->with('events')->whereHas('events', function ($query){
  				$query->where('start', '<>', '2017-11-01');
		})->get();

I am still seeing events for the date 2017-11-01. I am really stuck on this one

0

are you sure that the "start" column is date instead of string?

Last updated 7 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Walker Kinne Walker Kinne kinnedev Joined 23 Jun 2017

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.