Controller.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\SiteAlbumFolder;
  4. use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
  5. use Illuminate\Foundation\Validation\ValidatesRequests;
  6. use Illuminate\Routing\Controller as BaseController;
  7. class Controller extends BaseController
  8. {
  9. use AuthorizesRequests, ValidatesRequests;
  10. public $foldersTree = [];
  11. public function __construct()
  12. {
  13. // 一次性查询所有文件夹
  14. $folders = SiteAlbumFolder::all();
  15. // 构建父子关系树
  16. $fid = isset($_GET['fid'])? $_GET['fid'] : 0;
  17. $result = SiteAlbumFolder::buildTree($folders);
  18. $result = $result->toArray();
  19. foreach ($result as $key => $item) {
  20. $checked = false;
  21. foreach ($item['children'] as $child) {
  22. if ($fid == $child['id']) {
  23. $checked = true;
  24. }
  25. foreach ($child['children'] as $subChild) {
  26. if ($fid == $subChild['id']) {
  27. $checked = true;
  28. }
  29. }
  30. }
  31. if ($fid == $item['id']) {
  32. $checked = true;
  33. }
  34. $result[$key]['checked'] = $checked;
  35. }
  36. //dd($result);
  37. $this->foldersTree = $result;
  38. }
  39. }