2
0

Sentence.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace App\Models;
  3. use App\Services\PaliContentService;
  4. use Illuminate\Database\Eloquent\Factories\HasFactory;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Database\Eloquent\SoftDeletes;
  7. class Sentence extends Model
  8. {
  9. use HasFactory;
  10. use SoftDeletes;
  11. protected $fillable = [
  12. 'id',
  13. 'uid',
  14. 'book_id',
  15. 'paragraph',
  16. 'word_start',
  17. 'word_end',
  18. 'channel_uid',
  19. 'editor_uid',
  20. 'content',
  21. 'content_type',
  22. 'strlen',
  23. 'status',
  24. 'create_time',
  25. 'modify_time',
  26. 'language',
  27. ];
  28. protected $primaryKey = 'uid';
  29. protected $casts = [
  30. 'uid' => 'string',
  31. 'channel_uid' => 'string',
  32. ];
  33. protected static function booted(): void
  34. {
  35. $forget = function (Sentence $sentence) {
  36. PaliContentService::forgetParagraph(
  37. (int) $sentence->book_id,
  38. (int) $sentence->paragraph,
  39. (string) $sentence->channel_uid
  40. );
  41. };
  42. static::saved($forget);
  43. static::deleted($forget);
  44. static::restored($forget);
  45. static::forceDeleted($forget);
  46. }
  47. protected $dates = [
  48. 'created_at',
  49. 'updated_at',
  50. 'deleted_at',
  51. 'fork_at',
  52. ];
  53. /**
  54. * 获取句子所属的频道
  55. */
  56. public function channel()
  57. {
  58. return $this->belongsTo(Channel::class, 'channel_uid', 'uid');
  59. }
  60. public function scopeType($query, $type)
  61. {
  62. return $query->whereHas('channel', function ($q) use ($type) {
  63. $q->where('type', $type);
  64. });
  65. }
  66. public function scopeNissaya($query)
  67. {
  68. return $query->whereHas('channel', function ($q) {
  69. $q->where('type', 'nissaya');
  70. });
  71. }
  72. public function scopeLanguage($query, $language)
  73. {
  74. return $query->whereHas('channel', function ($q) use ($language) {
  75. $q->where('lang', $language);
  76. });
  77. }
  78. }