Index: app/Http/Controllers/Dashboard/FoldersController.php
===================================================================
--- app/Http/Controllers/Dashboard/FoldersController.php	(revision c6b84df06586305606df5b76204a64cdc63b09a2)
+++ app/Http/Controllers/Dashboard/FoldersController.php	(revision 4952f8e8349080ef7aaeef1678d5b2039f173d59)
@@ -39,4 +39,7 @@
                 $folders = $foldersInDeptSort->orderBy('name', 'asc')->paginate(12);
             }
+            else if ($request->query('sort') == 'no_of_files') {
+                $folders = $foldersInDeptSort->orderBy('no_of_files', 'desc')->paginate(12);
+            }
             else if($request->query('sort') == 'count'){
                 $total = $foldersInDeptSort->folder->files->count();
@@ -52,4 +55,7 @@
             else if ($request->query('sort') == 'name') {
                 $folders = Folder::orderBy('name', 'asc')->paginate(12);
+            }
+            else if ($request->query('sort') == 'no_of_files') {
+                $folders = Folder::orderBy('no_of_files', 'desc')->paginate(12);
             }
             else if ($request->query('sort') == 'important') {
@@ -97,5 +103,5 @@
             "deptName" => $deptName,
             "deptCode" => $deptCode,
-            "fileTypes" => '.' . implode(',.', explode(',', explode(':', FileType::find('1')->mimes)[1]))
+            "fileTypes" => '.' . implode(',.', explode(',', FileType::find('1')->mimes))
         ]);
 
@@ -117,4 +123,5 @@
         $folder->user()->associate($user);
         $folder->department()->associate($department);
+        $folder->department()->increment('no_of_folders');
 
         $folder->arch_id = $request->arch_id;
@@ -149,4 +156,5 @@
                 $newFile->location = $location . DIRECTORY_SEPARATOR . $fileName;
                 $newFile->folder()->associate($folder);
+                $newFile->folder()->increment('no_of_files');
                 $newFile->save();
             }
@@ -155,5 +163,5 @@
         Alert::flash("New folder created successfully");
 
-        return redirect()->route("dashboard.folders.index");
+        return redirect()->back();
     }
 
@@ -177,7 +185,7 @@
         $department = Department::find($request->department);
 
-        $folder->department()->associate($department);
-
-        $oldLocation = $folder->department->location . DIRECTORY_SEPARATOR . $folder->name;
+        $folder->department()->increment('no_of_folders');
+
+        $oldLocation = $folder->location;
 
         $folder->name = $request->name;
@@ -186,32 +194,44 @@
         $folder->updated_at = Carbon::now();
 
-        $location = $folder->department->location . DIRECTORY_SEPARATOR . $request->name;
-
-        if ($folder->isDirty('name')) {
-            if (!Storage::disk('uploads')->has($location)) {
-                Storage::disk('uploads')->move($oldLocation, $location);
-                foreach($files as $file){
-                    $file->location = $location . DIRECTORY_SEPARATOR . $file->name;
+       $newLocation = Department::find($request->department)->location . DIRECTORY_SEPARATOR . $request->name;
+
+        if($folder->department_id != $request->department){
+            $folder->department()->decrement('no_of_folders');
+            if (!Storage::disk('uploads')->has($newLocation)) {
+                Storage::disk('uploads')->move($oldLocation, $newLocation);
+                foreach($files as $file) {
+                    $file->location = $newLocation . DIRECTORY_SEPARATOR . $file->name;
                     $file->save();
                 }
             }
         }
+         if($folder->isDirty('name')) {
+            if (!Storage::disk('uploads')->has($newLocation)) {
+                Storage::disk('uploads')->move($oldLocation, $newLocation);
+                foreach($files as $file) {
+                    $file->location = $newLocation . DIRECTORY_SEPARATOR . $file->name;
+                    $file->save();
+                }
+            }
+        }
+
+        $folder->department()->associate($department);
+
+        $folder->location = $newLocation;
 
         if ($request->has('file_item')) {
             foreach ($request->file_item as $file) {
                 $fileName = $folder->name . '-' . uniqid() . '.' . $file->getClientOriginalExtension();
-                    $file->storeAs($location . DIRECTORY_SEPARATOR, $fileName);
+                    $file->storeAs($newLocation . DIRECTORY_SEPARATOR, $fileName);
                     $newFile = new File();
                     $newFile->name = $fileName;
-                    $newFile->location = $location . DIRECTORY_SEPARATOR . $fileName;
+                    $newFile->location = $newLocation . DIRECTORY_SEPARATOR . $fileName;
                     $newFile->folder()->associate($folder);
+                    $newFile->folder()->increment('no_of_files');
                     $newFile->save();
             }
         }
 
-        $folder->location = $location;
-
         $folder->save();
-
 
         Alert::flash("Folder edited successfully");
@@ -246,8 +266,10 @@
             $location = $folder->department->location . DIRECTORY_SEPARATOR . $folder->name;
             Storage::disk('uploads')->deleteDirectory($location);
+            $folder->department()->decrement('no_of_folders');
             Alert::flash($folder->name . " deleted successfully");
+            return redirect()->back();
         }
         Alert::flash($folder->name . " is important", "error");
-        return redirect()->route("dashboard.folders.index");
+        return redirect()->back();
     }
 
@@ -306,5 +328,5 @@
         }
         else {
-            $files = File::where('folder_id', $id)->paginate(10);
+            $files = File::where('folder_id', $id)->paginate(12);
         }
 
@@ -317,5 +339,5 @@
             "imageExt" => array("png", "jpg", "jpeg"),
             "folders" => $folders,
-            "fileTypes" => '.' . implode(',.', explode(',', explode(':', FileType::find('1')->mimes)[1]))
+            "fileTypes" => '.' . implode(',.', explode(',', FileType::find('1')->mimes))
         ]);
     }
