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\Relation; |
8
|
|
|
|
9
|
|
|
trait HelperMethodTrait |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Get the relations from a relation name |
14
|
|
|
* $relationName can be a single relation |
15
|
|
|
* Usage for User model: |
16
|
|
|
* parseRelationNames('customer') returns [$user->customer()] |
17
|
|
|
* parseRelationNames('customer.contact') returns [$user->customer(), $user->customer->contact()] |
18
|
|
|
* |
19
|
|
|
* @param string $relationName |
20
|
|
|
* @return Relation[] |
21
|
|
|
*/ |
22
|
|
|
private function parseRelationNames($relationName) |
23
|
|
|
{ |
24
|
|
|
$relationNames = explode('.', $relationName); |
25
|
|
|
$parentRelationName = null; |
26
|
|
|
$relations = []; |
27
|
|
|
|
28
|
|
|
foreach ($relationNames as $relationName) { |
29
|
|
|
if (is_null($parentRelationName)) { |
30
|
|
|
$relations[] = $this->$relationName(); |
31
|
|
|
$parentRelationName = $this->$relationName()->getRelated(); |
32
|
|
|
} else { |
33
|
|
|
$relations[] = $parentRelationName->$relationName(); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return $relations; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Add backticks to a table/column |
42
|
|
|
* |
43
|
|
|
* @param string $column |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
|
|
private function addBackticks($column) |
47
|
|
|
{ |
48
|
|
|
return preg_match('/^[0-9a-zA-Z\.]*$/', $column) ? |
49
|
|
|
'`' . str_replace(['`', '.'], ['', '`.`'], $column) . '`' : $column; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Return the sql for a query with the bindings replaced with the binding values |
54
|
|
|
* |
55
|
|
|
* @param Builder $builder |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
|
|
private function toSqlWithBindings(Builder $builder) |
59
|
|
|
{ |
60
|
|
|
$replacements = array_map('addslashes', $builder->getBindings()); |
61
|
|
|
$sql = $builder->toSql(); |
62
|
|
|
|
63
|
|
|
return preg_replace_callback( |
64
|
|
|
'/(\?)(?=(?:[^\'"]|["\'][^\'"]*["\'])*$)/', |
65
|
|
|
function () use (&$replacements) { |
66
|
|
|
return array_shift($replacements); |
67
|
|
|
}, |
68
|
|
|
$sql |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Add table name to column name if table name not already included in column name |
74
|
|
|
* |
75
|
|
|
* @param string $table |
76
|
|
|
* @param string $column |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
private function columnWithTableName($table, $column) |
80
|
|
|
{ |
81
|
|
|
return (preg_match('/(' . $table . '\.|`' . $table . '`)/i', $column) > 0 ? '' : $table . '.') . $column; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Remove a global scope if it exists |
86
|
|
|
* |
87
|
|
|
* @param Builder $query |
88
|
|
|
* @param string $scopeName |
89
|
|
|
* @return Builder |
90
|
|
|
*/ |
91
|
|
|
private function removeGlobalScope($query, $scopeName) |
92
|
|
|
{ |
93
|
|
|
/** @var Model $this */ |
94
|
|
|
$globalScopes = $this->getGlobalScopes(); |
95
|
|
|
if (isset($globalScopes[$scopeName])) { |
96
|
|
|
$query->withoutGlobalScope($scopeName); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $query; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Check if this model has already been joined to a table or relation |
104
|
|
|
* |
105
|
|
|
* @param Builder $builder |
106
|
|
|
* @param string $table |
107
|
|
|
* @param \Illuminate\Database\Eloquent\Relations\Relation $relation |
108
|
|
|
* @return bool |
109
|
|
|
*/ |
110
|
|
|
private function hasJoin(Builder $builder, $table, $relation) |
111
|
|
|
{ |
112
|
|
|
if (!$this->isJoined($builder, $table)) { |
113
|
|
|
return $this->isEagerLoaded($builder, $relation); |
114
|
|
|
} else { |
115
|
|
|
return true; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Check if model is currently joined to $table |
121
|
|
|
* |
122
|
|
|
* @param Builder $builder |
123
|
|
|
* @param string $table |
124
|
|
|
* @return bool |
125
|
|
|
*/ |
126
|
|
|
private function isJoined(Builder $builder, $table) |
127
|
|
|
{ |
128
|
|
|
$joins = $builder->getQuery()->joins; |
129
|
|
|
if (!is_null($joins)) { |
130
|
|
|
foreach ($joins as $joinClause) { |
131
|
|
|
if ($joinClause->table == $table) { |
132
|
|
|
return true; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return false; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Check if relation exists in eager loads |
142
|
|
|
* |
143
|
|
|
* @param Builder $builder |
144
|
|
|
* @param \Illuminate\Database\Eloquent\Relations\Relation $relation |
145
|
|
|
* @return bool |
146
|
|
|
*/ |
147
|
|
|
private function isEagerLoaded(Builder $builder, $relation) |
148
|
|
|
{ |
149
|
|
|
$eagerLoads = $builder->getEagerLoads(); |
150
|
|
|
|
151
|
|
|
return !is_null($eagerLoads) && in_array($relation, $eagerLoads); |
152
|
|
|
} |
153
|
|
|
} |