searchable; $query->where(function ($query) use ($fields, $search) { $query->where(array_shift($fields), 'LIKE', "%$search%"); $nameArray = explode(' ', $search); if (count($nameArray) >= 2) { $query->orWhere(function ($q) use ($nameArray) { $fname = array_shift($nameArray); $lastName = implode(' ', $nameArray); $q->where('first_name', 'LIKE', "$fname%"); $q->where('last_name', 'LIKE', "$lastName%"); }); } foreach ($fields as $field) { $query->orWhere($field, 'LIKE', "$search%"); } }); } return $query; } /** * Local scope to filter subscribers by search/query string * @param ModelQueryBuilder $query * @param array $statuses * @return ModelQueryBuilder */ public function scopeFilterByStatues($query, $statuses) { if ($statuses) { $query->whereIn('status', $statuses); } return $query; } /** * Accessor to get dynamic photo attribute * @return string */ public function getPhotoAttribute() { if (!empty($this->attributes['avatar'])) { return $this->attributes['avatar']; } $email = ''; if (isset($this->attributes['email'])) { $email = trim($this->attributes['email']); } $hash = md5(strtolower($email)); return apply_filters( 'fluent_support/get_avatar', "https://www.gravatar.com/avatar/${hash}?s=128", $email ); } /** * Accessor to get dynamic full_name attribute * @return string */ public function getFullNameAttribute() { $fname = isset($this->attributes['first_name']) ? $this->attributes['first_name'] : ''; $lname = isset($this->attributes['last_name']) ? $this->attributes['last_name'] : ''; return trim("{$fname} {$lname}"); } public static function explodeFullName($record) { if (!empty($record['first_name']) || !empty($record['last_name'])) { return $record; } if (!empty($record['full_name'])) { $fullNameArray = explode(' ', $record['full_name']); $record['first_name'] = array_shift($fullNameArray); if ($fullNameArray) { $record['last_name'] = implode(' ', $fullNameArray); } unset($record['full_name']); } return $record; } public function getUserProfileEditUrl() { $userEditUrl = ''; if ($this->user_id) { $userEditUrl = get_edit_profile_url($this->user_id); } return apply_filters('fluent_support/person_user_edit_url', $userEditUrl, $this); } public function getMeta($metaKey, $default = '') { $meta = Meta::where('object_id', $this->id) ->where('object_type', 'person_meta') ->where('key', $metaKey) ->first(); if ($meta) { $value = maybe_unserialize($meta->value); if ($value) { return $value; } } return $default; } public function updateMeta($metaKey, $metaValue) { $meta = Meta::where('object_id', $this->id) ->where('object_type', 'person_meta') ->where('key', $metaKey) ->first(); if ($meta) { $meta->value = maybe_serialize($metaValue); $meta->save(); } Meta::create([ 'object_type' => 'person_meta', 'object_id' => $this->id, 'key' => $metaKey, 'value' => maybe_serialize($metaValue) ]); return true; } public function deleteAllMeta() { Meta::where('object_id', $this->id) ->where('object_type', 'person_meta') ->delete(); } }