|
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
|
|
|
/** |
|
10
|
|
|
* Class RelatedPlusHelpers |
|
11
|
|
|
* |
|
12
|
|
|
* @property array order_fields |
|
13
|
|
|
* @property array order_defaults |
|
14
|
|
|
* @property array order_relations |
|
15
|
|
|
* @property array order_with |
|
16
|
|
|
* @property array search_fields |
|
17
|
|
|
* @property string connection |
|
18
|
|
|
*/ |
|
19
|
|
|
class RelatedPlusHelpers |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Return the sql for a query with the bindings replaced with the binding values |
|
23
|
|
|
* |
|
24
|
|
|
* @param Builder $builder |
|
25
|
|
|
* @return string |
|
26
|
|
|
*/ |
|
27
|
|
|
public static function toSqlWithBindings($builder) |
|
28
|
|
|
{ |
|
29
|
|
|
$replacements = array_map('addslashes', $builder->getBindings()); |
|
|
|
|
|
|
30
|
|
|
$sql = $builder->toSql(); |
|
31
|
|
|
|
|
32
|
|
|
foreach ($replacements as &$replacement) { |
|
33
|
|
|
if (!is_numeric($replacement)) { |
|
34
|
|
|
$replacement = '"' . $replacement . '"'; |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
return preg_replace_callback( |
|
39
|
|
|
'/(\?)(?=(?:[^\'"]|["\'][^\'"]*["\'])*$)/', |
|
40
|
|
|
function () use (&$replacements) { |
|
41
|
|
|
return array_shift($replacements); |
|
42
|
|
|
}, |
|
43
|
|
|
$sql |
|
44
|
|
|
); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Get table name from column name |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $column |
|
51
|
|
|
* @return string |
|
52
|
|
|
*/ |
|
53
|
|
|
public static function getTableFromColumn($column) |
|
54
|
|
|
{ |
|
55
|
|
|
$periodPos = strpos($column, '.'); |
|
56
|
|
|
|
|
57
|
|
|
return ($periodPos !== false ? substr($column, 0, $periodPos) : $column); |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Remove any global scopes which contain $scopeName in their name |
|
62
|
|
|
* |
|
63
|
|
|
* @param RelatedPlusTrait|Model $model |
|
64
|
|
|
* @param Builder $query |
|
65
|
|
|
* @param string $scopeName |
|
66
|
|
|
* @return Relation|Builder |
|
67
|
|
|
*/ |
|
68
|
|
|
public static function removeGlobalScopes($model, $query, $scopeName) |
|
69
|
|
|
{ |
|
70
|
|
|
$query->withoutGlobalScopes(collect($model->getGlobalScopes())->keys()->filter(function ( |
|
|
|
|
|
|
71
|
|
|
$value |
|
72
|
|
|
) use ($scopeName) { |
|
73
|
|
|
return stripos($value, $scopeName) !== false; |
|
74
|
|
|
})->toArray()); |
|
75
|
|
|
|
|
76
|
|
|
return $query; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|