AdminPermission.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace App\Models;
  3. class AdminPermission extends Model
  4. {
  5. protected $fillable = ['name', 'slug', 'http_method', 'http_path'];
  6. public static $httpMethods = [
  7. 'GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS', 'HEAD',
  8. ];
  9. public function setHttpMethodAttribute($method)
  10. {
  11. if (is_array($method)) {
  12. $this->attributes['http_method'] = implode(',', $method) ?: null;
  13. } else {
  14. $this->attributes['http_method'] = $method;
  15. }
  16. }
  17. public function getHttpMethodAttribute($method)
  18. {
  19. return array_filter(explode(',', $method));
  20. }
  21. public function setHttpPathAttribute($httpPath)
  22. {
  23. if (is_array($httpPath)) {
  24. $this->attributes['http_path'] = implode("\n", $httpPath) ?: null;
  25. } else {
  26. $this->attributes['http_path'] = $httpPath;
  27. }
  28. }
  29. public function getHttpPathAttribute($httpPath)
  30. {
  31. return array_filter(explode("\n", $httpPath));
  32. }
  33. public function roles()
  34. {
  35. return $this->belongsToMany(
  36. AdminRole::class,
  37. 'admin_role_permission',
  38. 'role_id',
  39. 'permission_id'
  40. );
  41. }
  42. }