refactor: kb & station & terminal
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
@@ -47,12 +48,9 @@ class Guide extends Model
|
||||
return $this->belongsTo(User::class, 'created_by');
|
||||
}
|
||||
|
||||
public function terminals()
|
||||
public function stations()
|
||||
{
|
||||
return $this->belongsToMany(Terminal::class, 'terminal_guides')
|
||||
->withPivot('priority')
|
||||
->withTimestamps()
|
||||
->orderBy('priority');
|
||||
return $this->belongsToMany(Station::class);
|
||||
}
|
||||
|
||||
public function scopePublished($query)
|
||||
@@ -65,6 +63,23 @@ class Guide extends Model
|
||||
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()
|
||||
|
||||
Reference in New Issue
Block a user