Departments.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. declare(strict_types=1);
  3. namespace Modules\Permissions\Models;
  4. use Catch\Base\CatchModel as Model;
  5. /**
  6. * @property $id
  7. * @property $parent_id
  8. * @property $department_name
  9. * @property $principal
  10. * @property $mobile
  11. * @property $email
  12. * @property $status
  13. * @property $sort
  14. * @property $creator_id
  15. * @property $created_at
  16. * @property $updated_at
  17. * @property $deleted_at
  18. */
  19. class Departments extends Model
  20. {
  21. protected $table = 'departments';
  22. protected $fillable = ['id', 'parent_id', 'department_name', 'principal', 'mobile', 'email', 'status', 'sort', 'creator_id', 'created_at', 'updated_at', 'deleted_at'];
  23. protected bool $isPaginate = false;
  24. /**
  25. * @var array
  26. */
  27. protected array $fields = ['id','parent_id','department_name','status','sort','created_at'];
  28. /**
  29. * @var array
  30. */
  31. protected array $form = ['parent_id','department_name','principal','mobile','email','sort'];
  32. /**
  33. * @var array
  34. */
  35. public array $searchable = [
  36. 'department_name' => 'like',
  37. 'status' => '=',
  38. ];
  39. protected bool $asTree = true;
  40. /**
  41. *
  42. * @param int|array $id
  43. * @return array
  44. */
  45. public function findFollowDepartments(int|array $id): array
  46. {
  47. if (!is_array($id)) {
  48. $id = [$id];
  49. }
  50. $followDepartmentIds = $this->whereIn($this->getParentIdColumn(), $id)->pluck('id')->toArray();
  51. if (! empty($followDepartmentIds)) {
  52. $followDepartmentIds = array_merge($followDepartmentIds, $this->findFollowDepartments($followDepartmentIds));
  53. }
  54. return $followDepartmentIds;
  55. }
  56. }