Sentence.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. }
  45. protected $dates = [
  46. 'created_at',
  47. 'updated_at',
  48. 'deleted_at',
  49. 'fork_at',
  50. ];
  51. /**
  52. * 获取句子所属的频道
  53. */
  54. public function channel()
  55. {
  56. return $this->belongsTo(Channel::class, 'channel_uid', 'uid');
  57. }
  58. public function scopeType($query, $type)
  59. {
  60. return $query->whereHas('channel', function ($q) use ($type) {
  61. $q->where('type', $type);
  62. });
  63. }
  64. public function scopeNissaya($query)
  65. {
  66. return $query->whereHas('channel', function ($q) {
  67. $q->where('type', 'nissaya');
  68. });
  69. }
  70. public function scopeLanguage($query, $language)
  71. {
  72. return $query->whereHas('channel', function ($q) use ($language) {
  73. $q->where('lang', $language);
  74. });
  75. }
  76. }