76 lines
1.8 KiB
PHP
76 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Spatie\Activitylog\Traits\LogsActivity;
|
|
use Spatie\Activitylog\LogOptions;
|
|
|
|
class Guide extends Model
|
|
{
|
|
use HasFactory, SoftDeletes, LogsActivity;
|
|
|
|
protected $fillable = [
|
|
'name',
|
|
'description',
|
|
'category',
|
|
'tags',
|
|
'status',
|
|
'created_by',
|
|
'published_at',
|
|
];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'tags' => 'array',
|
|
'published_at' => 'datetime',
|
|
];
|
|
}
|
|
|
|
public function pages()
|
|
{
|
|
return $this->hasMany(GuidePage::class)->orderBy('sort_order');
|
|
}
|
|
|
|
public function trunkPages()
|
|
{
|
|
return $this->hasMany(GuidePage::class)
|
|
->where('parent_id', -1)
|
|
->orderBy('sort_order');
|
|
}
|
|
|
|
public function creator()
|
|
{
|
|
return $this->belongsTo(User::class, 'created_by');
|
|
}
|
|
|
|
public function terminals()
|
|
{
|
|
return $this->belongsToMany(Terminal::class, 'terminal_guides')
|
|
->withPivot('priority')
|
|
->withTimestamps()
|
|
->orderBy('priority');
|
|
}
|
|
|
|
public function scopePublished($query)
|
|
{
|
|
return $query->where('status', 'published');
|
|
}
|
|
|
|
public function scopeCategory($query, string $category)
|
|
{
|
|
return $query->where('category', $category);
|
|
}
|
|
|
|
public function getActivitylogOptions(): LogOptions
|
|
{
|
|
return LogOptions::defaults()
|
|
->logOnly(['name', 'description', 'category', 'status'])
|
|
->logOnlyDirty()
|
|
->setDescriptionForEvent(fn(string $eventName) => "指引已{$eventName}");
|
|
}
|
|
}
|