| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- <?php
- namespace App\Models;
- use Illuminate\Database\Eloquent\Model;
- class Textbook extends Model
- {
- protected $table = 'textbooks';
- protected $fillable = [
- 'id',
- 'series_id',
- 'stage',
- 'schooling_system',
- 'grade',
- 'semester',
- 'naming_scheme',
- 'track',
- 'module_type',
- 'volume_no',
- 'legacy_code',
- 'curriculum_standard_year',
- 'curriculum_revision_year',
- 'approval_authority',
- 'approval_year',
- 'edition_label',
- 'official_title',
- 'aliases',
- 'isbn',
- 'cover_path',
- 'status',
- 'sort_order',
- 'meta',
- 'series',
- 'created_at',
- 'updated_at',
- ];
- protected $casts = [
- 'aliases' => 'array',
- 'meta' => 'array',
- 'grade' => 'integer',
- 'semester' => 'integer',
- 'volume_no' => 'integer',
- 'curriculum_standard_year' => 'integer',
- 'curriculum_revision_year' => 'integer',
- 'approval_year' => 'integer',
- 'sort_order' => 'integer',
- ];
- public function __construct(array $attributes = [])
- {
- parent::__construct($attributes);
- // 从 API 数据初始化时设置属性
- foreach ($attributes as $key => $value) {
- $this->setAttribute($key, $value);
- }
- }
- public function series()
- {
- return $this->belongsTo(TextbookSeries::class, 'series_id', 'id');
- }
- public function catalogs()
- {
- return $this->hasMany(TextbookCatalog::class);
- }
- /**
- * 获取系列信息(兼容 API 返回的嵌套对象)
- */
- public function getSeriesAttribute($value)
- {
- if (is_array($value)) {
- return (object) $value;
- }
- return $value;
- }
- public function getSeriesNameAttribute(): string
- {
- if ($this->relationLoaded('series') && $this->series) {
- return $this->series->name ?? '未归类系列';
- }
- $seriesId = $this->series_id;
- if (!$seriesId) {
- return '未归类系列';
- }
- static $seriesMap = null;
- if ($seriesMap === null) {
- $seriesMap = TextbookSeries::query()
- ->pluck('name', 'id')
- ->toArray();
- }
- return $seriesMap[$seriesId] ?? '未归类系列';
- }
- }
|