83 lines
1.7 KiB
PHP
83 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class Contact extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $fillable = [
|
|
'customer_id',
|
|
'is_primary',
|
|
'salutation',
|
|
'academic_title',
|
|
'first_name',
|
|
'last_name',
|
|
'job_title',
|
|
'email',
|
|
'phone',
|
|
'mobile_phone',
|
|
'avatar',
|
|
'online_accounts',
|
|
'description',
|
|
];
|
|
|
|
public function customer()
|
|
{
|
|
return $this->belongsTo(Customer::class);
|
|
}
|
|
|
|
/**
|
|
* Get the URL to the contact's avatar.
|
|
*/
|
|
public function getAvatarUrlAttribute()
|
|
{
|
|
if ($this->avatar) {
|
|
return asset('storage/' . $this->avatar);
|
|
}
|
|
|
|
// Return null if no avatar is set
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Scope a query to order contacts with primary contacts first.
|
|
*/
|
|
public function scopePrimaryFirst($query)
|
|
{
|
|
return $query->orderBy('is_primary', 'desc');
|
|
}
|
|
|
|
/**
|
|
* Set the online accounts attribute.
|
|
*
|
|
* @param array $value
|
|
* @return void
|
|
*/
|
|
public function setOnlineAccountsAttribute($value)
|
|
{
|
|
if (is_string($value)) {
|
|
$value = json_decode($value, true);
|
|
}
|
|
|
|
$this->attributes['online_accounts'] = json_encode([
|
|
'platform' => $value['platform'] ?? '',
|
|
'user_name' => $value['user_name'] ?? '',
|
|
'url' => $value['url'] ?? ''
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Get the online accounts attribute.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function getOnlineAccountsAttribute()
|
|
{
|
|
return json_decode($this->attributes['online_accounts'], true) ?? null;
|
|
}
|
|
}
|