AdminRole.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace App\Models\Admin;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Database\Eloquent\Relations\BelongsToMany;
  5. class AdminRole extends Model
  6. {
  7. protected $table = 'admin_roles';
  8. static $consultantSlug = 'consultant';
  9. protected $fillable = ['name', 'slug','subsite_id'];
  10. public function permissions()
  11. {
  12. return $this->belongsToMany(AdminPermission::class, 'admin_role_permissions', 'role_id', 'permission_id');
  13. }
  14. /**
  15. * A role belongs to many users.
  16. *
  17. * @return BelongsToMany
  18. */
  19. public function administrators() : BelongsToMany
  20. {
  21. $pivotTable = config('admin.database.role_users_table');
  22. $relatedModel = config('admin.database.users_model');
  23. return $this->belongsToMany($relatedModel, $pivotTable, 'role_id', 'user_id');
  24. }
  25. /**
  26. * Detach models from the relationship.
  27. *
  28. * @return void
  29. */
  30. protected static function boot()
  31. {
  32. parent::boot();
  33. static::deleting(function ($model) {
  34. $model->administrators()->detach();
  35. $model->permissions()->detach();
  36. });
  37. }
  38. public static function roleArr()
  39. {
  40. return self::when(get_subsite_id() > 0, function ($query) {
  41. $query->whereIn('subsite_id', [-1,get_subsite_id()]);
  42. })->pluck('name', 'id');
  43. }
  44. }