SearchFiltering.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace app\common\models;
  3. use Illuminate\Database\Eloquent\Builder;
  4. use Illuminate\Database\Eloquent\SoftDeletes;
  5. use Illuminate\Support\Collection;
  6. /**
  7. *
  8. */
  9. class SearchFiltering extends \app\common\models\BaseModel
  10. {
  11. use SoftDeletes;
  12. public $table = 'yz_search_filtering';
  13. protected $guarded = [];
  14. protected $hidden = [
  15. 'deleted_at',
  16. ];
  17. public function scopeGetFilterGroup($query, $parent_id = 0)
  18. {
  19. return $query->where('parent_id', $parent_id)->where('is_show', 0);
  20. }
  21. public function scopeCategoryLabel($query, $ids = [])
  22. {
  23. if ($ids && is_array($ids)) {
  24. return $query->whereIn('id', $ids);
  25. }
  26. return $query;
  27. }
  28. //获取全部标签
  29. public static function getAllFiltering()
  30. {
  31. $filtering = self::select('id', 'parent_id', 'name', 'is_front_show')->getFilterGroup()->get()->toArray();
  32. foreach ($filtering as $key => &$value) {
  33. $value['value'] = self::select('id', 'parent_id', 'name', 'is_front_show')->getFilterGroup($value['id'])->get()->toArray();
  34. }
  35. return $filtering;
  36. }
  37. // 获取所有可显示的标签
  38. public static function getAllEnableFiltering()
  39. {
  40. static $list;
  41. if ($list instanceof Collection) {
  42. return $list;
  43. }
  44. $filtering = SearchFiltering::getAllFiltering();
  45. $list = collect(array());
  46. foreach ($filtering as $v) {
  47. $list = $list->merge($v['value']);
  48. }
  49. return $list;
  50. }
  51. public static function boot()
  52. {
  53. parent::boot();
  54. static::addGlobalScope(function (Builder $builder) {
  55. $builder->uniacid();
  56. });
  57. }
  58. }