Files
KnowledgeBase/app/Models/Guide.php
2026-03-24 15:33:10 +08:00

99 lines
2.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
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);
}
public function edges()
{
return $this->hasMany(GuidePageEdge::class);
}
public function entryPage()
{
return $this->hasOne(GuidePage::class)
->whereNotIn('guide_pages.id', function ($q) {
$q->select('to_page_id')
->from('guide_page_edges')
->whereColumn('guide_page_edges.guide_id', 'guide_pages.guide_id');
});
}
public function creator()
{
return $this->belongsTo(User::class, 'created_by');
}
public function stations()
{
return $this->belongsToMany(Station::class);
}
public function scopePublished($query)
{
return $query->where('status', 'published');
}
public function scopeCategory($query, string $category)
{
return $query->where('category', $category);
}
/**
* 按用户线站过滤:全局 Guide无线站关联+ 用户线站关联的 Guide
*/
public function scopeAccessibleBy(Builder $query, User $user): Builder
{
if (!$user->hasStationRestriction()) {
return $query;
}
$stationIds = $user->getAccessibleStationIds();
return $query->where(function (Builder $q) use ($stationIds) {
$q->whereDoesntHave('stations')
->orWhereHas('stations', fn($sq) => $sq->whereIn('stations.id', $stationIds));
});
}
public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()
->logOnly(['name', 'description', 'category', 'status'])
->logOnlyDirty()
->setDescriptionForEvent(fn(string $eventName) => "指引已{$eventName}");
}
}