|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Blasttech\EloquentRelatedPlus; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
|
8
|
|
|
use Illuminate\Database\Eloquent\Relations\HasOneOrMany; |
|
9
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Trait RelatedPlusHelpers |
|
13
|
|
|
* |
|
14
|
|
|
* Helper functions |
|
15
|
|
|
* |
|
16
|
|
|
* @property array attributes |
|
17
|
|
|
* @property array nullable |
|
18
|
|
|
* @property array orderFields |
|
19
|
|
|
* @property array orderDefaults |
|
20
|
|
|
* @property array orderRelations |
|
21
|
|
|
* @property array orderWith |
|
22
|
|
|
* @property array searchFields |
|
23
|
|
|
* @property string connection |
|
24
|
|
|
* @method array getGlobalScopes() |
|
25
|
|
|
* |
|
26
|
|
|
*/ |
|
27
|
|
|
trait HelpersTrait |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* Return the sql for a query with the bindings replaced with the binding values |
|
31
|
|
|
* |
|
32
|
|
|
* @param Builder|\Illuminate\Database\Query\Builder $builder |
|
33
|
|
|
* @return string |
|
34
|
|
|
*/ |
|
35
|
|
|
public function toSqlWithBindings($builder) |
|
36
|
|
|
{ |
|
37
|
|
|
$replacements = $builder->getBindings(); |
|
38
|
|
|
$sql = $builder->toSql(); |
|
39
|
|
|
|
|
40
|
|
|
foreach ($replacements as &$replacement) { |
|
41
|
|
|
$replacement = addslashes($replacement); |
|
42
|
|
|
if (!is_numeric($replacement)) { |
|
43
|
|
|
$replacement = '"' . $replacement . '"'; |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return preg_replace_callback( |
|
48
|
|
|
'/(\?)(?=(?:[^\'"]|["\'][^\'"]*["\'])*$)/', |
|
49
|
|
|
function () use (&$replacements) { |
|
50
|
|
|
return array_shift($replacements); |
|
51
|
|
|
}, |
|
52
|
|
|
$sql |
|
53
|
|
|
); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Get table name from column name |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $column |
|
60
|
|
|
* @return string |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getTableFromColumn($column) |
|
63
|
|
|
{ |
|
64
|
|
|
$periodPos = strpos($column, '.'); |
|
65
|
|
|
|
|
66
|
|
|
return ($periodPos === false ? $column : substr($column, 0, $periodPos)); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Remove any global scopes which contain $scopeName in their name |
|
71
|
|
|
* |
|
72
|
|
|
* @param RelatedPlusTrait|Model $model |
|
73
|
|
|
* @param Relation|Builder $query |
|
74
|
|
|
* @param string $scopeName |
|
75
|
|
|
* @return Relation|BelongsTo|HasOneOrMany|Builder |
|
76
|
|
|
*/ |
|
77
|
|
|
public function removeGlobalScopes($model, $query, $scopeName) |
|
78
|
|
|
{ |
|
79
|
|
|
$query->withoutGlobalScopes(collect($model->getGlobalScopes())->keys()->filter(function ( |
|
80
|
|
|
$value |
|
81
|
|
|
) use ($scopeName) { |
|
82
|
|
|
return stripos($value, $scopeName) !== false; |
|
83
|
|
|
})->toArray()); |
|
84
|
|
|
|
|
85
|
|
|
return $query; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Set empty fields to null |
|
90
|
|
|
*/ |
|
91
|
|
|
public function setAttributesNull() |
|
92
|
|
|
{ |
|
93
|
|
|
/** @var Model $this */ |
|
94
|
|
|
foreach ($this->attributes as $key => $value) { |
|
95
|
|
|
if (isset($this->nullable[$key])) { |
|
96
|
|
|
$this->{$key} = empty(trim($value)) ? null : $value; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|