| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Models; |
| 4 |
|
| 5 |
use FluentSupport\App\Models\Traits\AgentTrait; |
| 6 |
|
| 7 |
class Agent extends Person |
| 8 |
{ |
| 9 |
use AgentTrait; |
| 10 |
|
| 11 |
protected static $type = 'agent'; |
| 12 |
|
| 13 |
protected $searchable = [ |
| 14 |
'id', |
| 15 |
'first_name', |
| 16 |
'last_name', |
| 17 |
'email', |
| 18 |
'address_line_1', |
| 19 |
'address_line_2', |
| 20 |
'country' |
| 21 |
]; |
| 22 |
|
| 23 |
/** |
| 24 |
* @return array |
| 25 |
*/ |
| 26 |
public function getSearchableFields(){ |
| 27 |
return $this->searchable; |
| 28 |
} |
| 29 |
|
| 30 |
public static function boot() |
| 31 |
{ |
| 32 |
parent::boot(); |
| 33 |
|
| 34 |
static::creating(function ($model) { |
| 35 |
$model->person_type = static::$type; |
| 36 |
$model->hash = md5(time().wp_generate_uuid4()); |
| 37 |
}); |
| 38 |
|
| 39 |
static::addGlobalScope(function ($builder) { |
| 40 |
$builder->where('person_type', 'agent'); |
| 41 |
}); |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
/** |
| 46 |
* One2Many: Customer has to many Click Tickets |
| 47 |
* @return Model Collection |
| 48 |
*/ |
| 49 |
public function tickets() |
| 50 |
{ |
| 51 |
$foreign_key = self::$type . '_id'; |
| 52 |
|
| 53 |
$class = __NAMESPACE__ . '\Ticket'; |
| 54 |
|
| 55 |
return $this->hasMany( |
| 56 |
$class, $foreign_key, 'id' |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
public function groups() |
| 61 |
{ |
| 62 |
return $this->belongsToMany( |
| 63 |
AgentGroup::class, 'fs_tag_pivot', 'source_id', 'tag_id' |
| 64 |
)->wherePivot('source_type', 'agent_group'); |
| 65 |
} |
| 66 |
|
| 67 |
public function scopeMentionBy($query, $search) |
| 68 |
{ |
| 69 |
if (!$search) { |
| 70 |
return $query; |
| 71 |
} |
| 72 |
|
| 73 |
return $query->where(function ($q) use ($search) { |
| 74 |
$q->where('first_name', 'LIKE', '%' . $search . '%') |
| 75 |
->orWhere('last_name', 'LIKE', '%' . $search . '%') |
| 76 |
->orWhere('email', 'LIKE', '%' . $search . '%'); |
| 77 |
}); |
| 78 |
} |
| 79 |
|
| 80 |
} |
| 81 |
|