1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Raystech\StarterKit\Traits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Validation\ValidationException; |
6
|
|
|
|
7
|
|
|
use Raystech\StarterKit\Supports\CustomQueryBuilder; |
8
|
|
|
|
9
|
|
|
trait DataViewer |
10
|
|
|
{ |
11
|
|
|
public function scopeTenantFilter($query) { |
12
|
|
|
$tenant_id = session()->get('_tenant', 1); |
13
|
|
|
|
14
|
|
|
return $query->whereHas('tenant', function($sub_query) use ($tenant_id) { |
15
|
|
|
$this->checkTenant($tenant_id, $sub_query); |
16
|
|
|
// $sub_query->where('tenant_id', 1); |
17
|
|
|
})->get(); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function checkTenant($tenant_id, $query) { |
21
|
|
|
return $query->where('tenant_id', $tenant_id); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function scopeAdvancedFilter($query) { |
25
|
|
|
return $this->process($query, request()->all()) |
26
|
|
|
->orderBy( |
27
|
|
|
request('order_column', 'created_at'), |
28
|
|
|
request('order_direction', 'desc') |
29
|
|
|
) |
30
|
|
|
->paginate(request('limit', 16)); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function process($query, $data) { |
34
|
|
|
|
35
|
|
|
$v = validator()->make($data, [ |
36
|
|
|
'order_column' => 'sometimes|required|in:'.$this->orderableColumns(), |
37
|
|
|
'order_direction' => 'sometimes|required|in:asc,desc', |
38
|
|
|
'limit' => 'sometimes|required|integer|min:1', |
39
|
|
|
|
40
|
|
|
'filter_match' => 'sometimes|required|in:and,or', |
41
|
|
|
'f' => 'sometimes|required|array', |
42
|
|
|
'f.*.column' => 'required|in:'.$this->whiteListColumns(), |
43
|
|
|
'f.*.operator' => 'required_with:f.*.column|in:'.$this->allowedOperators(), |
44
|
|
|
'f.*.query_1' => 'required', |
45
|
|
|
'f.*.query_2' => 'required_if:f.*.operator,between,not_between' |
46
|
|
|
]); |
47
|
|
|
|
48
|
|
|
if($v->fails()) { |
49
|
|
|
return dd($v->messages()->all()); |
|
|
|
|
50
|
|
|
throw new ValidationException; |
|
|
|
|
51
|
|
|
} |
52
|
|
|
$data['allowedFilters'] = $this->allowedFilters; |
53
|
|
|
return (new CustomQueryBuilder)->apply($query, $data); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
protected function whiteListColumns() { |
57
|
|
|
return implode(',', $this->allowedFilters); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
protected function orderableColumns() { |
61
|
|
|
return implode(',', $this->orderable); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
protected function allowedOperators() { |
65
|
|
|
return implode(',', [ |
66
|
|
|
'equal_to', |
67
|
|
|
'not_equal_to', |
68
|
|
|
'less_than', |
69
|
|
|
'greater_than', |
70
|
|
|
'between', |
71
|
|
|
'not_between', |
72
|
|
|
'contains', |
73
|
|
|
'starts_with', |
74
|
|
|
'ends_with', |
75
|
|
|
'in_the_past', |
76
|
|
|
'in_the_next', |
77
|
|
|
'in_the_period', |
78
|
|
|
'less_than_count', |
79
|
|
|
'greater_than_count', |
80
|
|
|
'equal_to_count', |
81
|
|
|
'not_equal_to_count' |
82
|
|
|
]); |
83
|
|
|
} |
84
|
|
|
} |
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.