SiteAlbumFolder.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace App\Admin\Repositories;
  3. use App\Models\SiteAlbumFolder as Model;
  4. use Dcat\Admin\Repositories\EloquentRepository;
  5. class SiteAlbumFolder extends EloquentRepository
  6. {
  7. /**
  8. * Model.
  9. *
  10. * @var string
  11. */
  12. protected $eloquentClass = Model::class;
  13. /*
  14. * 改写列表显示返回的数据
  15. */
  16. public function get(Grid\Model|\Dcat\Admin\Grid\Model $model)
  17. {
  18. // 获取模型对象
  19. $obj = $this->model();
  20. // 获取数据
  21. $tree = $obj->orderBy('order', 'desc')->orderBy('id', 'desc')->get();
  22. $treeData = $tree->toArray();
  23. // 构建树形结构
  24. $data=buildTree($treeData);
  25. // 转换成一维数组
  26. $data=flattenTree($data);
  27. return $model->makePaginator(
  28. count($data), // 传入总记录数
  29. $data // 传入数据二维数组
  30. );
  31. }
  32. public static function selectMainOptions(\Closure $closure = null)
  33. {
  34. if (!$closure) {
  35. $closure = function ($query) {
  36. $query = $query->orderBy('order', 'desc')->orderBy('id', 'desc');
  37. return $query;
  38. };
  39. }
  40. $selectOptions = Model::class::selectOptions($closure);
  41. //只显示2层
  42. foreach ($selectOptions as $key => $value) {
  43. if (substr_count($value,'&nbsp;') > 8) {
  44. unset($selectOptions[$key]);
  45. }
  46. }
  47. return $selectOptions;
  48. }
  49. // 调用模型方法
  50. public static function selectOptions(\Closure $closure = null)
  51. {
  52. if (!$closure) {
  53. $closure = function ($query) {
  54. $query = $query->where('enabled', 1);
  55. $query = $query->orderBy('order', 'desc')->orderBy('id', 'desc');
  56. return $query;
  57. };
  58. }
  59. $selectOptions = Model::class::selectOptions($closure);
  60. return $selectOptions;
  61. }
  62. }