Pencarian fasih dari kolom tabel anak
Member::whereHas('membership', function ($q) {
$q->where('expiration', 'like', 'somethingToSearchFor');
})->get();
Anxious Alpaca
Member::whereHas('membership', function ($q) {
$q->where('expiration', 'like', 'somethingToSearchFor');
})->get();
$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}%");
}
}
}