Files
dockerfluxodecaixapro/modules/contact/classes/Models/Contact.php
T
2026-09-01 10:46:29 -03:00

153 lines
3.1 KiB
PHP
Executable File

<?php
namespace Modules\Contact\Models;
use Illuminate\Database\Eloquent\Model;
class Contact extends Model
{
/**
* The table associated with model.
*
* @var string
*/
protected $table = 'contacts';
/**
* The primary key associated with table.
*
* @var string
*/
protected $primaryKey = 'contacts_id';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'contacts_name',
'contacts_email',
'contacts_phone',
'contacts_subject',
'contacts_message',
'contacts_response',
'contacts_status',
'contacts_ip_address',
'contacts_user_agent',
'contacts_read_at'
];
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'contacts_read_at' => 'datetime',
'created_at' => 'datetime',
'updated_at' => 'datetime'
];
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = [
'contacts_read_at',
'created_at',
'updated_at'
];
/**
* Get unread contacts only.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeUnread($query)
{
return $query->whereNull('contacts_read_at');
}
/**
* Get read contacts only.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeRead($query)
{
return $query->whereNotNull('contacts_read_at');
}
/**
* Get contacts by status.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param string $status
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeStatus($query, string $status)
{
return $query->where('contacts_status', $status);
}
/**
* Mark contact as read.
*
* @return bool
*/
public function markAsRead(): bool
{
if ($this->contacts_read_at) {
return true;
}
return $this->update(['contacts_read_at' => \Carbon\Carbon::now()]);
}
/**
* Mark contact as unread.
*
* @return bool
*/
public function markAsUnread(): bool
{
return $this->update(['contacts_read_at' => null]);
}
/**
* Check if contact is read.
*
* @return bool
*/
public function isRead(): bool
{
return !is_null($this->contacts_read_at);
}
/**
* Get formatted created date.
*
* @return string
*/
public function getFormattedCreatedAtAttribute(): string
{
return $this->created_at->format('d/m/Y H:i');
}
/**
* Get short message (100 characters).
*
* @return string
*/
public function getShortMessageAttribute(): string
{
return strlen($this->contacts_message) > 100
? substr($this->contacts_message, 0, 100) . '...'
: $this->contacts_message;
}
}