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

Размер файла: 1.92Kb
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Models;
  6.  
  7. use Illuminate\Database\Eloquent\Model;
  8. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  9. use Illuminate\Database\Eloquent\Relations\Relation;
  10.  
  11. /**
  12. * Class BaseModel
  13. *
  14. * @property User user
  15. * @method increment(string $field, $amount = 1, array $extra = [])
  16. * @method decrement(string $field, $amount = 1, array $extra = [])
  17. * @package App\Models
  18. */
  19. class BaseModel extends Model
  20. {
  21. /**
  22. * The attributes that should be cast to native types.
  23. *
  24. * @var array
  25. */
  26. protected $casts = [
  27. 'user_id' => 'int',
  28. ];
  29.  
  30. /**
  31. * Bootstrap services.
  32. *
  33. * @return void
  34. */
  35. public static function boot()
  36. {
  37. parent::boot();
  38.  
  39. Relation::morphMap([
  40. Down::$morphName => Down::class,
  41. Article::$morphName => Article::class,
  42. Photo::$morphName => Photo::class,
  43. Offer::$morphName => Offer::class,
  44. News::$morphName => News::class,
  45. Topic::$morphName => Topic::class,
  46. Post::$morphName => Post::class,
  47. Guestbook::$morphName => Guestbook::class,
  48. Message::$morphName => Message::class,
  49. Wall::$morphName => Wall::class,
  50. Comment::$morphName => Comment::class,
  51. Vote::$morphName => Vote::class,
  52. Item::$morphName => Item::class,
  53. ]);
  54. }
  55.  
  56. /**
  57. * Возвращает связь пользователей
  58. *
  59. * @return BelongsTo
  60. */
  61. public function user(): BelongsTo
  62. {
  63. return $this->belongsTo(User::class, 'user_id')->withDefault();
  64. }
  65.  
  66. /**
  67. * Возвращает логин пользователя
  68. *
  69. * @param string|null $value
  70. *
  71. * @return string
  72. */
  73. public function getLoginAttribute($value): string
  74. {
  75. return $value ?? setting('deleted_user');
  76. }
  77. }