Department.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace App\Models;
  3. use App\Http\Requests\IndexHelper;
  4. use App\Models\Enums\DisplayIndexObjectType;
  5. use App\Models\Scopes\CompanyScope;
  6. use EloquentFilter\Filterable;
  7. use Illuminate\Database\Eloquent\Factories\HasFactory;
  8. use Illuminate\Database\Eloquent\Model;
  9. class Department extends Model
  10. {
  11. use HasFactory,Filterable;
  12. protected $table = 'department';
  13. protected $fillable = [
  14. 'name',
  15. 'parent_id',
  16. 'company_id',
  17. 'manager_id',
  18. ];
  19. protected static function booted()
  20. {
  21. parent::booted(); // TODO: Change the autogenerated stub
  22. static::addGlobalScope(new CompanyScope);
  23. static::creating(function (Department $department){
  24. $type =DisplayIndexObjectType::DEPARTMENT;
  25. $displayIndex = IndexHelper::getObjectMaxIndex($type);
  26. $department->display_id =$displayIndex;
  27. });
  28. }
  29. public function children()
  30. {
  31. return $this->hasMany(Department::class, 'parent_id');
  32. }
  33. public function users(){
  34. return $this->hasMany(User::class, 'department_id');
  35. }
  36. }