-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathUntitled.php
135 lines (110 loc) · 4.87 KB
/
Untitled.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOneOrMany;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
$x = new ComplexDemoTable(1);
$columns = Mediconesystems\LivewireDatatables\ColumnSet::build($x->columns())
->include($x->include)
->exclude($x->exclude)
->hide($x->hide)
->formatDates($x->dates)
->formatTimes($x->times)
->search($x->searchable)
->sort($x->sort)->columns;
$query = User::query();
$columns->each(
function ($column) use ($query) {
/// SELECT STATEMENT
//callback field
if (Str::startsWith($column->name, 'callback_')) {
// $query->addSelect($query->getModel()->getTable() . '.' . $column->name);
return;
}
// base table field
if (!Str::contains($column->name, '.')) {
$query->addSelect(
$query->getModel()->getTable() . '.' . $column->name
);
} else {
// some kind of nested field
if (method_exists($query->getModel(), Str::before($column->name, '.'))) {
$parent = $query;
$joins = explode('.', Str::beforeLast($column->name, '.'));
// dd($joins);
foreach ($joins as $i => $join) {
dump($i, $join);
$relation = method_exists($parent->getModel(), $join)
? $parent->getRelation($join)
: $parent;
dump($relation->toSql());
switch (true) {
case $relation instanceof HasOne:
$query->leftJoinIfNotJoined(
$relation->getRelated()->getTable(),
$relation->getQualifiedForeignKeyName(),
$relation->getQualifiedParentKeyName()
);
dump('hey1');
$parent = $relation;
break;
case $relation instanceof BelongsTo:
$query->leftJoinIfNotJoined(
$relation->getRelated()->getTable(),
$relation->getQualifiedOwnerKeyName(),
$relation->getQualifiedForeignKeyName()
);
dump('hey2');
$parent = $relation;
break;
case $relation instanceof HasMany:
$aggregate = $column->aggregate();
$name = explode('.', $column->name);
$query->withAggregate(
$name[0],
$aggregate,
$name[1]
);
}
}
$field = Str::afterLast($column->name, '.');
}
}
/// JOINS
/// ORDER BY
/// WHERE
}
// ->dd()
);
$query->dd();
$query->get();
// switch (true) {
// case $parent instanceof HasOne:
// $query->addSelect(
// $parent->getRelated()->getTable() .
// '.' .
// $field .
// ' AS ' .
// $column->name
// );
// break;
// case $parent instanceof BelongsTo:
// $query->addSelect(
// $parent->getRelated()->getTable() .
// '.' .
// $field .
// ' AS ' .
// $column->name
// );
// break;
// case $parent instanceof HasMany:
// $aggregate = $column->aggregate();
// $name = explode('.', $column->name);
// $query->withAggregate(
// $name[0],
// $aggregate,
// $name[1]
// );
// break;
// }