1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- <?php
- namespace App\Models;
- use App\Http\Requests\IndexHelper;
- use App\Models\Enums\DisplayIndexObjectType;
- use App\Models\Scopes\CompanyScope;
- use EloquentFilter\Filterable;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Model;
- class Department extends Model
- {
- use HasFactory,Filterable;
- protected $table = 'department';
- protected $fillable = [
- 'name',
- 'parent_id',
- 'company_id',
- 'manager_id',
- ];
- protected static function booted()
- {
- parent::booted(); // TODO: Change the autogenerated stub
- static::addGlobalScope(new CompanyScope);
- static::creating(function (Department $department){
- $type =DisplayIndexObjectType::DEPARTMENT;
- $displayIndex = IndexHelper::getObjectMaxIndex($type);
- $department->display_id =$displayIndex;
- });
- }
- public function children()
- {
- return $this->hasMany(Department::class, 'parent_id');
- }
- public function users(){
- return $this->hasMany(User::class, 'department_id');
- }
- }
|