Category.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace App\Repositories\Models\CMS;
  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\CMS;
  10. */
  11. class Category extends Model implements Transformable
  12. {
  13. use TransformableTrait;
  14. protected $table = 'cms_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 static function sonGroup($id)
  26. {
  27. $str = [];
  28. $num = self::query()->where('parent_id', '=', $id)->pluck('id')->toArray();
  29. if (count($num)) {
  30. foreach ($num as $k => $v) {
  31. $str[] = $v;
  32. $ids = self::sonGroup($v);
  33. foreach ($ids as $i) {
  34. $str[] = $i;
  35. }
  36. }
  37. }
  38. return $str;
  39. }
  40. public static function byRoleGetIds($role_id)
  41. {
  42. if (login_admin_id() == 1) {
  43. return self::query()->pluck('id')->toArray();
  44. }
  45. return self::query()->where('role_ids', 'like', "%-{$role_id}-%")->orWhere('role_ids', 'like', "%-0-%")->pluck('id')->toArray();
  46. }
  47. public function getParentNameAttribute()
  48. {
  49. return self::query()->where('id', $this->attributes['parent_id'])->value('name') ?? '--';
  50. }
  51. public function setRoleIdsAttribute($val)
  52. {
  53. $this->attributes['role_ids'] = '-' . arr2str0($val, '-') . '-';
  54. }
  55. public function getRoleIdsAttribute($val)
  56. {
  57. return str2arr0(trim($val, '-'), '-');
  58. }
  59. }