account_id; if (preg_match('/(.{1,})[:](.{1,})/', $needle, $matches)) { $search_field = $matches[1]; $search_term = $matches[2]; $field = ContactFieldType::where('account_id', $accountId) ->where('name', 'LIKE', $search_field) ->first(); $field_id = is_null($field) ? 0 : $field->id; return Contact::whereHas('contactFields', function ($query) use ($accountId, $field_id, $search_term) { $query->where([ ['account_id', $accountId], ['data', 'like', "$search_term%"], ['contact_field_type_id', $field_id], ]); }) ->orderBy($orderByColumn, $orderByDirection); } return Contact::search($needle, $accountId, $orderByColumn, $orderByDirection); } }