Skip to content

Commit e269ca2

Browse files
committed
init
1 parent d363403 commit e269ca2

File tree

5 files changed

+334
-0
lines changed

5 files changed

+334
-0
lines changed

.editorconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
insert_final_newline = true
6+
indent_style = space
7+
indent_size = 4
8+
trim_trailing_whitespace = true
9+
10+
[*.md]
11+
trim_trailing_whitespace = false
12+
13+
[*.yml]
14+
indent_style = space
15+
indent_size = 2

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor
2+
.idea
3+

composer.json

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{
2+
"name": "westhack/laravel-eloquent-search",
3+
"description": "Laravel Eloquent Search",
4+
"keywords": [
5+
"laravel",
6+
"eloquent",
7+
"model",
8+
"search",
9+
"query",
10+
"filter"
11+
],
12+
"license": "MIT",
13+
"authors": [
14+
{
15+
"name": "westhack",
16+
"email": "wencun9527@gmail.com"
17+
}
18+
],
19+
"require": {
20+
"php": ">=7",
21+
"illuminate/console": "~5.0",
22+
"illuminate/filesystem": "~5.0",
23+
"illuminate/database": "~5.0",
24+
"illuminate/support": "~5.0",
25+
"illuminate/config": "~5.0",
26+
"illuminate/pagination": "~5.0"
27+
},
28+
"require-dev": {
29+
"phpunit/phpunit": "~5.4.0",
30+
"mockery/mockery": "^0.9.5"
31+
},
32+
"autoload": {
33+
"psr-4": {
34+
"EloquentSearch\\": "src/"
35+
}
36+
},
37+
"autoload-dev": {
38+
},
39+
"config": {
40+
"preferred-install": "dist"
41+
},
42+
"minimum-stability": "dev",
43+
"prefer-stable": true,
44+
"extra": {
45+
"laravel": {
46+
"providers": [
47+
"EloquentSearch\\ServiceProvider"
48+
]
49+
}
50+
}
51+
}

src/SearchTrait.php

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
<?php
2+
3+
namespace EloquentSearch;
4+
5+
trait SearchTrait
6+
{
7+
/**
8+
* WHERE $column LIKE %$value% query.
9+
*
10+
* @param \Illuminate\Database\Eloquent\Builder $query
11+
* @param $column
12+
* @param $value
13+
* @param string $boolean
14+
*
15+
* @return \Illuminate\Database\Eloquent\Builder
16+
*/
17+
public function scopeWhereLike($query, $column, $value, $boolean = 'and')
18+
{
19+
return $query->where($column, 'LIKE', "%$value%", $boolean);
20+
}
21+
22+
/**
23+
* WHERE $column LIKE $value% query.
24+
*
25+
* @param \Illuminate\Database\Eloquent\Builder $query
26+
* @param $column
27+
* @param $value
28+
* @param string $boolean
29+
*
30+
* @return \Illuminate\Database\Eloquent\Builder
31+
*/
32+
public function scopeWhereBeginsWith($query, $column, $value, $boolean = 'and')
33+
{
34+
return $query->where($column, 'LIKE', "$value%", $boolean);
35+
}
36+
37+
/**
38+
* WHERE $column LIKE %$value query.
39+
*
40+
* @param \Illuminate\Database\Eloquent\Builder $query
41+
* @param $column
42+
* @param $value
43+
* @param string $boolean
44+
*
45+
* @return \Illuminate\Database\Eloquent\Builder
46+
*/
47+
public function scopeWhereEndsWith($query, $column, $value, $boolean = 'and')
48+
{
49+
return $query->where($column, 'LIKE', "%$value", $boolean);
50+
}
51+
52+
/**
53+
* @param array $params
54+
* @param \Illuminate\Database\Eloquent\Builder $query
55+
*
56+
* @return \Illuminate\Database\Eloquent\Builder
57+
*/
58+
public function scopeSearch($query, $params)
59+
{
60+
if (is_array($params)) {
61+
foreach ($params as $key => $param) {
62+
if (! array_has($param, ['column', 'operator', 'values'])) {
63+
continue;
64+
}
65+
66+
if ($param['values'] == null) {
67+
continue;
68+
}
69+
70+
switch (strtolower($param['operator'])) {
71+
case 'between':
72+
$query->whereBetween($param['column'], $param['values']);
73+
break;
74+
case 'not between':
75+
$query->whereNotBetween($param['column'], $param['values']);
76+
break;
77+
case 'is null':
78+
$query->whereNull($param['column']);
79+
break;
80+
case 'is not null':
81+
$query->whereNotNull($param['column']);
82+
break;
83+
case 'like all':
84+
$query->whereLike($param['column'], $param['values']);
85+
break;
86+
case 'begin with':
87+
$query->whereBeginsWith($param['column'], $param['values']);
88+
break;
89+
case 'end with':
90+
$query->whereEndsWith($param['column'], $param['values']);
91+
break;
92+
case 'in':
93+
$query->whereIn($param['column'], $param['values']);
94+
break;
95+
default:
96+
$query->where($param['column'], $param['operator'], $param['values']);
97+
}
98+
}
99+
}
100+
101+
return $query;
102+
}
103+
104+
/**
105+
* @param \Illuminate\Database\Eloquent\Builder $query
106+
* @param array $groups
107+
*
108+
* @return \Illuminate\Database\Eloquent\Builder
109+
*/
110+
public function scopeGroupSearch($query, array $groups)
111+
{
112+
$t = class_basename($this);
113+
114+
if (is_array($groups)) {
115+
$new_groups = [];
116+
foreach ($groups as $key => $group) {
117+
$arr = explode('.', $key);
118+
$new_groups[$arr[0]] = $group;
119+
}
120+
121+
unset($groups);
122+
123+
foreach ($new_groups as $key => $params) {
124+
if ($key == $this->getTable() || strtolower($key) == strtolower($t)) {
125+
$query->search($params);
126+
} else {
127+
if (!$this->checkValueIsNull($params)) {
128+
$query->whereHas($key, function ($query) use ($params) {
129+
$query->search($params);
130+
});
131+
}
132+
}
133+
}
134+
}
135+
136+
return $query;
137+
}
138+
139+
/**
140+
* @param array $params
141+
* @return bool
142+
*/
143+
private function checkValueIsNull(array $params)
144+
{
145+
foreach ($params as $key => $param) {
146+
if ($param['values'] != '') {
147+
return false;
148+
}
149+
}
150+
151+
return true;
152+
}
153+
}

src/SortOrderTrait.php

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
3+
namespace EloquentSearch;
4+
5+
trait SortOrderTrait
6+
{
7+
public $sortable = [];
8+
9+
public $sortOrderAsc = 'ascend';
10+
11+
public $sortOrderDesc = 'descend';
12+
13+
/**
14+
* 排序
15+
*
16+
* @param \Illuminate\Database\Eloquent\Builder $query
17+
* @param array $sort
18+
*
19+
* @return \Illuminate\Database\Eloquent\Builder
20+
*/
21+
public function scopeSortOrder($query, $sort)
22+
{
23+
$sort = array_wrap($sort);
24+
25+
$sortable = $this->getSortable();
26+
if (!empty($sort)) {
27+
$column = array_get($sort, 'column');
28+
if (array_get($sort, 'order')) {
29+
$order = $this->formatSort(array_get($sort, 'order'));
30+
} else {
31+
$order = $this->formatSort(array_get($sortable, $column));
32+
}
33+
34+
if ($column != '' && $order != '') {
35+
$query->orderBy($column, $order);
36+
}
37+
} else {
38+
$column = head(array_keys($sortable));
39+
$order = $this->formatSort(array_get($sortable, $column, $this->sortOrderAsc));
40+
41+
$query->orderBy($column, $order);
42+
}
43+
44+
return $query;
45+
}
46+
47+
public function getSortable()
48+
{
49+
if (empty($this->sortable)) {
50+
return [$this->getKeyName() => $this->sortOrderDesc];
51+
}
52+
53+
$sortable = [];
54+
foreach ($this->sortable as $key => $value) {
55+
if (is_int($key)) {
56+
$sortable[$value] = $this->sortOrderDesc;
57+
} else {
58+
$sortable[$key] = ($value == $this->sortOrderAsc ? $this->sortOrderAsc : $this->sortOrderDesc);
59+
}
60+
}
61+
62+
return $sortable;
63+
}
64+
65+
public function setSortable($column, $order)
66+
{
67+
$sortable = [];
68+
if (is_string($column)) {
69+
$sortable[$column] = ($order == $this->sortOrderAsc ? $this->sortOrderAsc : $this->sortOrderDesc);
70+
} else if(is_array($column)) {
71+
foreach ($column as $key => $value) {
72+
if (is_int($key)) {
73+
$sortable[$value] = $this->sortOrderDesc;
74+
} else {
75+
$sortable[$key] = ($value == $this->sortOrderAsc ? $this->sortOrderAsc : $this->sortOrderDesc);
76+
}
77+
}
78+
}
79+
$this->sortable = $sortable;
80+
81+
return $this;
82+
}
83+
84+
public function addSortable($column, $order)
85+
{
86+
$sortable = [];
87+
if (is_string($column)) {
88+
$sortable[$column] = ($order == $this->sortOrderAsc ? $this->sortOrderAsc : $this->sortOrderDesc);
89+
} else if(is_array($column)) {
90+
foreach ($column as $key => $value) {
91+
if (is_int($key)) {
92+
$sortable[$value] = $this->sortOrderDesc;
93+
} else {
94+
$sortable[$key] = ($value == $this->sortOrderAsc ? $this->sortOrderAsc : $this->sortOrderDesc);
95+
}
96+
}
97+
}
98+
99+
$this->sortable = array_merge($this->sortable, $sortable);
100+
101+
return $this;
102+
}
103+
104+
private function formatSort($sort)
105+
{
106+
if ($sort == $this->sortOrderAsc) {
107+
return 'asc';
108+
}
109+
110+
return 'desc';
111+
}
112+
}

0 commit comments

Comments
 (0)