瀏覽代碼

feat: ✨ Add Datatables Sortable trait

IceToast 2 年之前
父節點
當前提交
b25ce96efb
共有 1 個文件被更改,包括 21 次插入0 次删除
  1. 21 0
      app/Traits/DatatablesSortable.php

+ 21 - 0
app/Traits/DatatablesSortable.php

@@ -0,0 +1,21 @@
+<?php
+
+namespace App\Traits;
+
+trait DatatablesSortable
+{
+
+    public function sortByColumn($order, $columns, $query)
+    {
+        // order is an array like [{"column":"11","dir":"asc"}]
+        if ($order) {
+            $order = $order[0];
+            $column = $columns[$order['column']]['data'];
+            $direction = $order['dir'];
+
+            $query->orderBy($column, $direction);
+        }
+
+        return $query;
+    }
+}