So, I'm using polymorphic relation on User model. UserManager and UserAgent models related to User model with their own specific data. UserManager model has company_id, I'm using eager loading to filter models that only has one of the $companies values.
$companies = [1, 4];
$users = User::with(['userable' => function ($query) use ($companies) {
$query->whereIn('company_id', $companies);
}])->whereUserableType(UserManager::class)->get();
This code works fine for filtering result, the only problem is is still returns models that doesn't match with the query, but they return as 'userable' = null.
I know I can filter through those results like this:
function filterResult($data)
{
for ($i = 0; $i < sizeof($data); $i++)
if (is_null($data[$i]->userable))
unset($data[$i]);
return $data;
}
But is there any better way or maybe "Eloquent way" to filter out results with 'userable' = null value?
Thank you for your time.