Textbook.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. class Textbook extends Model
  5. {
  6. protected $table = 'textbooks';
  7. protected $fillable = [
  8. 'id',
  9. 'series_id',
  10. 'stage',
  11. 'schooling_system',
  12. 'grade',
  13. 'semester',
  14. 'naming_scheme',
  15. 'track',
  16. 'module_type',
  17. 'volume_no',
  18. 'legacy_code',
  19. 'curriculum_standard_year',
  20. 'curriculum_revision_year',
  21. 'approval_authority',
  22. 'approval_year',
  23. 'edition_label',
  24. 'official_title',
  25. 'display_title',
  26. 'aliases',
  27. 'isbn',
  28. 'cover_path',
  29. 'status',
  30. 'sort_order',
  31. 'meta',
  32. 'series',
  33. 'created_at',
  34. 'updated_at',
  35. ];
  36. protected $casts = [
  37. 'aliases' => 'array',
  38. 'meta' => 'array',
  39. 'grade' => 'integer',
  40. 'semester' => 'integer',
  41. 'volume_no' => 'integer',
  42. 'curriculum_standard_year' => 'integer',
  43. 'curriculum_revision_year' => 'integer',
  44. 'approval_year' => 'integer',
  45. 'sort_order' => 'integer',
  46. ];
  47. public function __construct(array $attributes = [])
  48. {
  49. parent::__construct($attributes);
  50. // 从 API 数据初始化时设置属性
  51. foreach ($attributes as $key => $value) {
  52. $this->setAttribute($key, $value);
  53. }
  54. }
  55. public function series()
  56. {
  57. return $this->belongsTo(TextbookSeries::class);
  58. }
  59. public function catalogs()
  60. {
  61. return $this->hasMany(TextbookCatalog::class);
  62. }
  63. /**
  64. * 获取系列信息(兼容 API 返回的嵌套对象)
  65. */
  66. public function getSeriesAttribute($value)
  67. {
  68. if (is_array($value)) {
  69. return (object) $value;
  70. }
  71. return $value;
  72. }
  73. }