Instructions to handle sorting files by time for library laravel file manager
- 24-07-2022
- Toanngo92
- 0 Comments
In a recent project, I needed to load files in "time DESC" order when laravel-filemanager's iframe was called. (By default, files in laravel file manager are sorted alphabetically.) The problem will occur when the number of files is too large, the user cannot find the file he just uploaded.
When reading the docs, I didn't see the custom options in this situation, so it took a bit more time to learn on stack overflow to solve the problem, so I rewrote this article today to save you time on research, although From my point of view, this is a bad solution because you have to directly edit the code in the vendor directory, there will be problems when updating, so please comment on the project so that the following code brothers understand the maintenance flow.
Step 1: access the file vendor/unisharp/laravel-filemanager/public/js/script.js
Edit the following variable to change the request parameter when calling the server to get files:
var sort_type = 'alphabetic';
Edited into:
var sort_type = 'time';
Step 2: access the file vendor/unisharp/laravel-filemanager/src/Controllers/ItemsController.php
Comment or delete the old getItems() function of the ItemsController.php file
/*public function getItems() { $currentPage = self::getCurrentPageFromRequest(); $perPage = $this->helper->getPaginationPerPage(); $items = array_merge($this->lfm->folders(), $this->lfm->files()); return [ 'items' => array_map(function ($item) { return $item->fill()->attributes; }, array_slice($items, ($currentPage - 1) * $perPage, $perPage)), 'paginator' => [ 'current_page' => $currentPage, 'total' => count($items), 'per_page' => $perPage, ], 'display' => $this->helper->getDisplayMode(), 'working_dir' => $this->lfm->path('working_dir'), ]; }*/
Edit the source code to:
use IlluminateHttpRequest; // định nghĩa kiểu dữ liệu request để hứng tham số từ request // sửa hàm getItems trong Controller public function getItems(Request $request) { /* dd($request) => nếu dump biến này ra, chúng ta sẽ thấy parameter url được thay đổi từ alphabetic thành time nếu sửa đúng */ $currentPage = self::getCurrentPageFromRequest(); $perPage = $this->helper->getPaginationPerPage(); $files = $this->lfm->files(); if($request->sort_type=='time'){ $files = array_reverse($files); } $items = array_merge($this->lfm->folders(), $files); return [ 'items' => array_map(function ($item) { return $item->fill()->attributes; }, array_slice($items, ($currentPage - 1) * $perPage, $perPage)), 'paginator' => [ 'current_page' => $currentPage, 'total' => count($items), 'per_page' => $perPage, ], 'display' => $this->helper->getDisplayMode(), 'working_dir' => $this->lfm->path('working_dir'), ]; }
So ok, good luck!
Refer to the link for details: https://stackoverflow.com/questions/47003766/laravel-filemanager-sort-by-time-default