Department.php 840 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. /**
  3. * Created by IntelliJ IDEA.
  4. * User: kelyliang
  5. * Date: 2024/2/18
  6. * Time: 下午 03:52
  7. */
  8. namespace App\Models;
  9. use Illuminate\Database\Eloquent\Factories\HasFactory;
  10. use Illuminate\Database\Eloquent\Model;
  11. class Department extends Model
  12. {
  13. use HasFactory;
  14. protected $table = 'department';
  15. // 设置主键(可选,默认是'id')
  16. protected $primaryKey = 'id';
  17. // 定义允许批量赋值的字段(可选)
  18. protected $fillable = ['name', 'parent_id','manager_id'];
  19. // 例如,一个部门可能有多个子部门
  20. public function children()
  21. {
  22. return $this->hasMany(Department::class, 'parent_id');
  23. }
  24. // 或者,一个部门可能属于另一个部门
  25. public function parent()
  26. {
  27. return $this->belongsTo(Department::class, 'parent_id');
  28. }
  29. }