1
|
|
|
<?php namespace Distilleries\Expendable\Models; |
2
|
|
|
|
3
|
|
|
use Distilleries\Expendable\Helpers\Security; |
4
|
|
|
use Distilleries\Expendable\Models\Traits\ReservedKeyWord; |
5
|
|
|
use Distilleries\Expendable\Models\Traits\ReservedKeyWordTrait; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use \DB; |
8
|
|
|
use \Exception; |
9
|
|
|
|
10
|
|
|
class BaseModel extends Model implements ReservedKeyWord |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
use ReservedKeyWordTrait; |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
public static function getChoice() |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
$data = self::all(); |
20
|
|
|
$result = []; |
21
|
|
|
foreach ($data as $item) { |
22
|
|
|
$result[$item['id']] = isset($item['libelle']) ? $item['libelle'] : $item['id']; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
return $result; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function scopeSearch($query, $searchQuery) |
29
|
|
|
{ |
30
|
|
|
|
31
|
|
|
return $query->where(function ($query) use ($searchQuery) { |
32
|
|
|
$columns = $this->getAllColumnsNames(); |
33
|
|
|
|
34
|
|
|
foreach ($columns as $column) { |
35
|
|
|
$column = $this->isReserved($column) ? '"' . $column . '"' : $column; |
36
|
|
|
|
37
|
|
|
if ((DB::connection()->getDriverName()) == 'oracle') { |
38
|
|
|
$query->orWhereRaw('LOWER(' . $column . ') like ? ESCAPE \'\\\'', ['%' . Security::escapeLike(strtolower($searchQuery)) . '%']); |
39
|
|
|
} else { |
40
|
|
|
$query->orwhere($column, 'like', '%' . Security::escapeLike($searchQuery,'\\\'') . '%'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
} |
44
|
|
|
}); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function getAllColumnsNames() |
48
|
|
|
{ |
49
|
|
|
switch (DB::connection()->getDriverName()) { |
50
|
|
|
case 'pgsql': |
51
|
|
|
$query = "SELECT column_name FROM information_schema.columns WHERE table_name = '" . $this->getTable() . "'"; |
52
|
|
|
$column_name = 'column_name'; |
53
|
|
|
$reverse = true; |
54
|
|
|
break; |
55
|
|
|
|
56
|
|
|
case 'mysql': |
57
|
|
|
$query = 'SHOW COLUMNS FROM ' . $this->getTable(); |
58
|
|
|
$column_name = 'Field'; |
59
|
|
|
$reverse = false; |
60
|
|
|
break; |
61
|
|
|
|
62
|
|
|
case 'sqlsrv': |
63
|
|
|
$parts = explode('.', $this->getTable()); |
64
|
|
|
$num = (count($parts) - 1); |
65
|
|
|
$table = $parts[$num]; |
66
|
|
|
$query = "SELECT column_name FROM " . DB::connection()->getConfig('database') . ".INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = N'" . $table . "'"; |
67
|
|
|
$column_name = 'column_name'; |
68
|
|
|
$reverse = false; |
69
|
|
|
break; |
70
|
|
|
case 'oracle': |
71
|
|
|
$query = 'SELECT COLUMN_NAME from ALL_TAB_COLUMNS WHERE TABLE_NAME=\'' . strtoupper($this->getTable()) . '\' AND DATA_TYPE <> \'CLOB\' AND DATA_TYPE <> \'NUMBER\' AND DATA_TYPE <> \'TIMESTAMP\''; |
72
|
|
|
$column_name = 'column_name'; |
73
|
|
|
$reverse = false; |
74
|
|
|
break; |
75
|
|
|
default: |
76
|
|
|
$error = 'Database driver not supported: ' . DB::connection()->getConfig('driver'); |
77
|
|
|
throw new Exception($error); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$columns = []; |
81
|
|
|
|
82
|
|
|
foreach (DB::select($query) as $column) { |
83
|
|
|
$columns[] = $column->$column_name; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if ($reverse) { |
87
|
|
|
$columns = array_reverse($columns); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $columns; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function scopeBetweenCreate($query, $start, $end) |
94
|
|
|
{ |
95
|
|
|
return $query->whereBetween($this->getTable() . '.created_at', [$start, $end]); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function scopeBetweenUpdate($query, $start, $end) |
99
|
|
|
{ |
100
|
|
|
return $query->whereBetween($this->getTable() . '.updated_at', [$start, $end]); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
} |