PreQuestion.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  6. class PreQuestion extends Model
  7. {
  8. use HasFactory;
  9. protected $fillable = [
  10. 'candidate_id',
  11. 'import_id',
  12. 'sequence',
  13. 'index',
  14. 'raw_markdown',
  15. 'stem',
  16. 'options',
  17. 'images',
  18. 'tables',
  19. 'source',
  20. ];
  21. protected $casts = [
  22. 'candidate_id' => 'integer',
  23. 'sequence' => 'integer',
  24. 'index' => 'integer',
  25. 'options' => 'array',
  26. 'images' => 'array',
  27. 'tables' => 'array',
  28. 'source' => 'array',
  29. 'created_at' => 'datetime',
  30. 'updated_at' => 'datetime',
  31. ];
  32. public function import(): BelongsTo
  33. {
  34. return $this->belongsTo(MarkdownImport::class, 'import_id');
  35. }
  36. public function getFirstImageAttribute(): ?string
  37. {
  38. if (empty($this->images)) {
  39. return null;
  40. }
  41. $images = is_string($this->images) ? json_decode($this->images, true) : $this->images;
  42. return $images[0] ?? null;
  43. }
  44. public function getSourceListAttribute(): string
  45. {
  46. if (empty($this->source)) {
  47. return 'No source';
  48. }
  49. $source = is_string($this->source) ? json_decode($this->source, true) : $this->source;
  50. return implode(', ', $source);
  51. }
  52. public function getStemPreviewAttribute(): string
  53. {
  54. return \Str::limit($this->stem, 100);
  55. }
  56. }