Просмотр файла app/Models/Dialogue.php

Размер файла: 1.59Kb
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Models;
  6.  
  7. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  8.  
  9. /**
  10. * Class Dialogue
  11. *
  12. * @property int $id
  13. * @property int $message_id
  14. * @property int $user_id
  15. * @property int $author_id
  16. * @property string $type
  17. * @property int $reading
  18. * @property int $created_at
  19. *
  20. * @property-read User $author
  21. * @property-read Message $message
  22. */
  23. class Dialogue extends BaseModel
  24. {
  25. public const IN = 'in'; // Принятые
  26. public const OUT = 'out'; // Отправленные
  27.  
  28. /**
  29. * Indicates if the model should be timestamped.
  30. *
  31. * @var bool
  32. */
  33. public $timestamps = false;
  34.  
  35. /**
  36. * The attributes that aren't mass assignable.
  37. *
  38. * @var array
  39. */
  40. protected $guarded = [];
  41.  
  42. /**
  43. * Возвращает связь пользователей
  44. *
  45. * @return BelongsTo
  46. */
  47. public function author(): BelongsTo
  48. {
  49. return $this->belongsTo(User::class, 'author_id')->withDefault();
  50. }
  51.  
  52. /**
  53. * Message
  54. *
  55. * @return BelongsTo
  56. */
  57. public function message(): BelongsTo
  58. {
  59. return $this->belongsTo(Message::class, 'message_id')->withDefault();
  60. }
  61.  
  62. /**
  63. * Удаление собщений диалога
  64. *
  65. * @return bool|null
  66. */
  67. public function delete(): ?bool
  68. {
  69. // Если сообщение осталось только у одного пользователя
  70. if ($this->message->dialogues->count() === 1) {
  71. $this->message->delete();
  72. }
  73.  
  74. return parent::delete();
  75. }
  76. }