소스 검색

增加"Filter\Between::toTimestamp"方法

jqh 5 년 전
부모
커밋
68a9138b01
4개의 변경된 파일62개의 추가작업 그리고 2개의 파일을 삭제
  1. 11 1
      src/Controllers/LogController.php
  2. 1 1
      src/Grid/Column/Condition.php
  3. 25 0
      src/Grid/Column/Filter/Between.php
  4. 25 0
      src/Grid/Filter/Between.php

+ 11 - 1
src/Controllers/LogController.php

@@ -75,9 +75,19 @@ class LogController extends Controller
                     return $userModel::findOrFail($v)->pluck('name', 'id');
                 });
 
-            $filter->equal('method', trans('admin.method'))->select(array_combine(OperationLogModel::$methods, OperationLogModel::$methods));
+            $filter->equal('method', trans('admin.method'))
+                ->select(
+                    array_combine(OperationLogModel::$methods, OperationLogModel::$methods)
+                );
+
             $filter->like('path', trans('admin.uri'));
+
             $filter->equal('ip', 'IP');
+
+            $filter->between('created_at')
+                ->width(4)
+                ->datetime();
+
         });
 
         return $grid;

+ 1 - 1
src/Grid/Column/Condition.php

@@ -105,7 +105,7 @@ class Condition
             $condition = $this->call($condition);
         }
 
-        return$this->result = $condition ? true : false;
+        return $this->result = $condition ? true : false;
     }
 
     public function getResult()

+ 25 - 0
src/Grid/Column/Filter/Between.php

@@ -10,6 +10,11 @@ class Between extends Filter
 {
     protected $dateFormat = null;
 
+    /**
+     * @var bool
+     */
+    protected $timestamp = false;
+
     public function __construct()
     {
         $this->class = [
@@ -18,6 +23,18 @@ class Between extends Filter
         ];
     }
 
+    /**
+     * Convert the datetime into unix timestamp.
+     *
+     * @return $this
+     */
+    public function toTimestamp()
+    {
+        $this->timestamp = true;
+
+        return $this;
+    }
+
     /**
      * Date filter.
      *
@@ -75,6 +92,14 @@ class Between extends Filter
             return;
         }
 
+        if ($this->timestamp) {
+            $value = array_map(function ($v) {
+                if ($v) {
+                    return strtotime($v);
+                }
+            }, $value);
+        }
+
         if (!isset($value['start'])) {
             return $model->where($this->getColumnName(), '<=', $value['end']);
         }

+ 25 - 0
src/Grid/Filter/Between.php

@@ -13,6 +13,23 @@ class Between extends AbstractFilter
      */
     protected $view = 'admin::filter.between';
 
+    /**
+     * @var bool
+     */
+    protected $timestamp = false;
+
+    /**
+     * Convert the datetime into unix timestamp.
+     *
+     * @return $this
+     */
+    public function toTimestamp()
+    {
+        $this->timestamp = true;
+
+        return $this;
+    }
+
     /**
      * Format id.
      *
@@ -73,6 +90,14 @@ class Between extends AbstractFilter
             return $val !== '';
         });
 
+        if ($this->timestamp) {
+            $value = array_map(function ($v) {
+                if ($v) {
+                    return strtotime($v);
+                }
+            }, $value);
+        }
+
         if (empty($value)) {
             return;
         }