BaseModel.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | saiadmin [ saiadmin快速开发框架 ]
  4. // +----------------------------------------------------------------------
  5. // | Author: sai <1430792918@qq.com>
  6. // +----------------------------------------------------------------------
  7. namespace plugin\saiadmin\basic\eloquent;
  8. use support\Model;
  9. use Illuminate\Database\Eloquent\SoftDeletes;
  10. use plugin\saiadmin\basic\contracts\ModelInterface;
  11. /**
  12. * Laravel Eloquent 模型基类
  13. */
  14. class BaseModel extends Model implements ModelInterface
  15. {
  16. use SoftDeletes;
  17. /**
  18. * 创建时间字段
  19. */
  20. const CREATED_AT = 'create_time';
  21. /**
  22. * 更新时间字段
  23. */
  24. const UPDATED_AT = 'update_time';
  25. /**
  26. * 删除时间字段
  27. */
  28. const DELETED_AT = 'delete_time';
  29. /**
  30. * 隐藏字段
  31. * @var array
  32. */
  33. protected $hidden = ['delete_time'];
  34. /**
  35. * 不可批量赋值的属性 (为空表示全部可赋值)
  36. * @var array
  37. */
  38. protected $guarded = [];
  39. /**
  40. * 类型转换
  41. * @return array
  42. */
  43. protected function casts(): array
  44. {
  45. return [
  46. 'create_time' => 'datetime:Y-m-d H:i:s',
  47. 'update_time' => 'datetime:Y-m-d H:i:s',
  48. ];
  49. }
  50. /**
  51. * 处理时区问题
  52. * @param \DateTimeInterface $date
  53. * @return string
  54. */
  55. protected function serializeDate(\DateTimeInterface $date): string
  56. {
  57. return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
  58. }
  59. /**
  60. * 获取表名
  61. * @return string
  62. */
  63. public function getTableName(): string
  64. {
  65. return $this->getTable();
  66. }
  67. /**
  68. * 获取主键名
  69. * @return string
  70. */
  71. public function getPrimaryKeyName(): string
  72. {
  73. return $this->getKeyName();
  74. }
  75. /**
  76. * 搜索器搜索
  77. * @param array $fields
  78. * @param array $data
  79. * @return mixed
  80. */
  81. public function withSearch(array $fields, array $data): mixed
  82. {
  83. $query = $this->newQuery();
  84. foreach ($fields as $field) {
  85. $method = 'search' . ucfirst($this->toCamelCase($field)) . 'Attr';
  86. if (method_exists($this, $method) && isset($data[$field]) && $data[$field] !== '') {
  87. $this->$method($query, $data[$field]);
  88. } else {
  89. $query->where($field, $data[$field]);
  90. }
  91. }
  92. return $query;
  93. }
  94. /**
  95. * 将下划线命名转换为驼峰命名
  96. * @param string $str
  97. * @return string
  98. */
  99. protected function toCamelCase(string $str): string
  100. {
  101. return lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $str))));
  102. }
  103. /**
  104. * 添加时间范围搜索
  105. * @param $query
  106. * @param $value
  107. */
  108. public function searchCreateTimeAttr($query, $value)
  109. {
  110. if (is_array($value)) {
  111. $query->whereBetween('create_time', $value);
  112. } else {
  113. $query->where('create_time', '=', $value);
  114. }
  115. }
  116. /**
  117. * 更新时间范围搜索
  118. * @param mixed $query
  119. * @param mixed $value
  120. */
  121. public function searchUpdateTimeAttr($query, $value)
  122. {
  123. if (is_array($value)) {
  124. $query->whereBetween('update_time', $value);
  125. } else {
  126. $query->where('update_time', '=', $value);
  127. }
  128. }
  129. /**
  130. * 模型启动事件
  131. * @return void
  132. */
  133. protected static function boot(): void
  134. {
  135. parent::boot();
  136. // 创建前事件
  137. static::creating(function ($model) {
  138. $info = getCurrentInfo();
  139. $schema = $model->getConnection()->getSchemaBuilder();
  140. if ($info && $schema->hasColumn($model->getTable(), 'created_by')) {
  141. $model->created_by = $info['id'];
  142. }
  143. });
  144. // 保存前事件
  145. static::saving(function ($model) {
  146. $info = getCurrentInfo();
  147. $schema = $model->getConnection()->getSchemaBuilder();
  148. if ($info && $schema->hasColumn($model->getTable(), 'updated_by')) {
  149. $model->updated_by = $info['id'];
  150. }
  151. });
  152. }
  153. }