59 lines
1.2 KiB
PHP
59 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Support\Str;
|
|
|
|
class ChatSession extends Model
|
|
{
|
|
protected $fillable = [
|
|
'session_token',
|
|
'user_id',
|
|
'category_id',
|
|
'ip_address',
|
|
'user_agent',
|
|
'last_activity_at',
|
|
];
|
|
|
|
protected $casts = [
|
|
'last_activity_at' => 'datetime',
|
|
];
|
|
|
|
// Auto-generate session token
|
|
protected static function booted(): void
|
|
{
|
|
static::creating(function (ChatSession $session) {
|
|
if (empty($session->session_token)) {
|
|
$session->session_token = Str::random(48);
|
|
}
|
|
});
|
|
}
|
|
|
|
// === Relationships ===
|
|
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
public function category(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Category::class);
|
|
}
|
|
|
|
public function logs(): HasMany
|
|
{
|
|
return $this->hasMany(ChatLog::class)->orderBy('created_at');
|
|
}
|
|
|
|
// === Helpers ===
|
|
|
|
public function touch($attribute = null): bool
|
|
{
|
|
return $this->update(['last_activity_at' => now()]);
|
|
}
|
|
}
|