|
1
|
|
|
<?php namespace App\LaravelRestCms; |
|
2
|
|
|
|
|
3
|
|
|
trait SearchTrait { |
|
4
|
|
|
|
|
5
|
|
|
public static $labelCol = []; |
|
6
|
|
|
public static $searchCols = []; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Adds search methods to the model |
|
10
|
|
|
* |
|
11
|
|
|
* @param string $keyword |
|
12
|
|
|
* @param string $labelCol |
|
13
|
|
|
* @return \App\LaravelRestCms\BaseModel |
|
14
|
|
|
*/ |
|
15
|
|
|
public function addSearch($keyword = null, $labelCol = null) |
|
16
|
|
|
{ |
|
17
|
|
|
$searchCols = self::getSearchCols(null, $labelCol); |
|
18
|
|
|
$keyword = strtolower($keyword); |
|
19
|
|
|
|
|
20
|
|
|
if ($searchCols && !is_null($keyword) && trim($keyword) != '') { |
|
21
|
|
|
|
|
22
|
|
|
return self::orWhere(function($query) use ($searchCols, $keyword) |
|
23
|
|
|
{ |
|
24
|
|
|
foreach ($searchCols as $key=>$val) { |
|
25
|
|
|
$query->orWhere($val, 'like', "%{$keyword}%"); |
|
26
|
|
|
} |
|
27
|
|
|
}); |
|
28
|
|
|
} else { |
|
29
|
|
|
return $this; |
|
30
|
|
|
} |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Retrieves the columns to search |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $override |
|
37
|
|
|
* @param string $default |
|
38
|
|
|
* @return array |
|
39
|
|
|
*/ |
|
40
|
|
|
public function getSearchCols($override = null, $default = null) |
|
41
|
|
|
{ |
|
42
|
|
|
if (!is_null($override)) { |
|
43
|
|
|
$searchCols = $override; |
|
44
|
|
|
} else if (!is_null(static::$searchCols)) { |
|
45
|
|
|
$searchCols = static::$searchCols; |
|
46
|
|
|
} else if (!is_null(static::$labelCol)) { |
|
47
|
|
|
$searchCols = static::$labelCol; |
|
48
|
|
|
} else if (!is_null($default)) { |
|
49
|
|
|
$searchCols = $default; |
|
50
|
|
|
} else { |
|
51
|
|
|
return false; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return (array) $searchCols; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Eloquent scope of the search |
|
59
|
|
|
* |
|
60
|
|
|
* @param \App\LaravelRestCms\BaseModel $query |
|
61
|
|
|
* @param string $keyword |
|
62
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
|
63
|
|
|
*/ |
|
64
|
|
|
public function scopeSearch(\App\LaravelRestCms\BaseModel $query, $keyword = null) |
|
65
|
|
|
{ |
|
66
|
|
|
$searchCols = $this->getSearchCols(null, static::$labelCol); |
|
67
|
|
|
$query->select('*', implode(' ', static::$labelCol) . ' as label', 'url'); |
|
|
|
|
|
|
68
|
|
|
$match = ''; |
|
69
|
|
|
|
|
70
|
|
|
// make search results use the "and" clause |
|
71
|
|
|
$keyword[0] = '"' . $keyword[0] . '"'; |
|
72
|
|
|
|
|
73
|
|
|
if (is_array($searchCols)) { |
|
74
|
|
|
foreach ($searchCols as $searchCol) { |
|
75
|
|
|
$match = "MATCH(" . $searchCol . ") AGAINST (? IN BOOLEAN MODE)"; |
|
76
|
|
|
$query = $query->whereRaw($match, $keyword); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return $query->orderByRaw($match . ' desc', $keyword); |
|
81
|
|
|
} |
|
82
|
|
|
} |
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: