eloquent search from child table column
Member::whereHas('membership', function ($q) {
$q->where('expiration', 'like', 'somethingToSearchFor');
})->get();
eloquent search from child table column
Member::whereHas('membership', function ($q) {
$q->where('expiration', 'like', 'somethingToSearchFor');
})->get();
eloquent search from child table column
$fields = array('membership' => ['expiration'], 'firstname', 'middlename', 'lastname', 'email', 'dlnumber', 'membership_id');
// orWhereHas will use joins, so we'll start with fields foreach
foreach ($fields as $relation => $field)
{
if (is_array($field))
{
// here we join table for each relation
$query->orWhereHas($relation, function ($q) use ($field, $search) {
// here we need to use nested where like: ... WHERE key = fk AND (x LIKE y OR z LIKE y)
$q->where(function ($q) use ($field, $search) {
foreach ($field as $relatedField)
{
foreach ($search as $term)
{
$q->orWhere($relatedField, 'like', "%{$term}%");
}
}
});
});
}
else
{
foreach ($search as $term)
{
$query->orWhere($field, 'like', "%{$term}%");
}
}
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us