Category.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace App\Repositories\Models\Inform;
  3. use App\Repositories\Models\Model;
  4. use Prettus\Repository\Contracts\Transformable;
  5. use Prettus\Repository\Traits\TransformableTrait;
  6. /**
  7. * Class Category.
  8. *
  9. * @package namespace App\Repositories\Models\Inform;
  10. */
  11. class Category extends Model implements Transformable
  12. {
  13. use TransformableTrait;
  14. protected $table = 'inform_categories';
  15. /**
  16. * The attributes that are mass assignable.
  17. *
  18. * @var array
  19. */
  20. protected $guarded = [];
  21. protected static function booted()
  22. {
  23. self::language();
  24. }
  25. public function getParentNameAttribute()
  26. {
  27. return self::query()->where('id', $this->attributes['parent_id'])->value('name') ?? '--';
  28. }
  29. public function setRoleIdsAttribute($val)
  30. {
  31. $this->attributes['role_ids'] = '-' . arr2str0($val, '-') . '-';
  32. }
  33. public function getRoleIdsAttribute($val)
  34. {
  35. return str2arr0(trim($val, '-'), '-');
  36. }
  37. public static function sonGroup($id)
  38. {
  39. $str = [];
  40. $num = self::query()->where('parent_id', '=', $id)->pluck('id')->toArray();
  41. if (count($num)) {
  42. foreach ($num as $k => $v) {
  43. $str[] = $v;
  44. $ids = self::sonGroup($v);
  45. foreach ($ids as $i) {
  46. $str[] = $i;
  47. }
  48. }
  49. }
  50. return $str;
  51. }
  52. public static function byRoleGetIds($role_id)
  53. {
  54. if (login_admin_id() == 1) {
  55. return self::query()->pluck('id')->toArray();
  56. }
  57. return self::query()->where('role_ids', 'like', "%-{$role_id}-%")->orWhere('role_ids', 'like', "%-0-%")->pluck('id')->toArray();
  58. }
  59. }