Skip to content

Commit b0b9618

Browse files
author
Marius
committed
fix eager loading retroactively laravel/framework#51825
1 parent ef3d94d commit b0b9618

File tree

1 file changed

+85
-37
lines changed

1 file changed

+85
-37
lines changed

src/Eloquent/CustomRelations/HasCleverRelationships.php

Lines changed: 85 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Illuminate\Database\Eloquent\Relations\MorphOne;
1616
use Illuminate\Database\Eloquent\Relations\MorphTo;
1717
use Illuminate\Database\Eloquent\Relations\MorphToMany;
18+
use MacropaySolutions\LaravelCrudWizard\Models\BaseModel;
1819

1920
/**
2021
* @see HasRelationships
@@ -29,12 +30,17 @@ trait HasCleverRelationships
2930
*/
3031
protected function newHasOne(Builder $query, Model $parent, $foreignKey, $localKey): HasOne
3132
{
32-
return new class($query, $parent, $foreignKey, $localKey) extends HasOne {
33+
return new class ($query, $parent, $foreignKey, $localKey, $this) extends HasOne {
3334
use RelationCleverTrait;
3435

35-
public function __construct(Builder $query, Model $parent, string $foreignKey, string $localKey)
36-
{
37-
$this->setConstraintsStaticFlag($parent);
36+
public function __construct(
37+
Builder $query,
38+
Model $parent,
39+
string $foreignKey,
40+
string $localKey,
41+
BaseModel $resourceModel
42+
) {
43+
$this->setConstraintsStaticFlag($resourceModel);
3844

3945
return parent::__construct($query, $parent, $foreignKey, $localKey);
4046
}
@@ -53,14 +59,15 @@ protected function newHasOneThrough(
5359
$localKey,
5460
$secondLocalKey
5561
): HasOneThrough {
56-
return new class(
62+
return new class (
5763
$query,
5864
$farParent,
5965
$throughParent,
6066
$firstKey,
6167
$secondKey,
6268
$localKey,
63-
$secondLocalKey
69+
$secondLocalKey,
70+
$this
6471
) extends HasOneThrough {
6572
use RelationCleverTrait;
6673

@@ -71,9 +78,10 @@ public function __construct(
7178
string $firstKey,
7279
string $secondKey,
7380
string $localKey,
74-
string $secondLocalKey
81+
string $secondLocalKey,
82+
BaseModel $resourceModel
7583
) {
76-
$this->setConstraintsStaticFlag($throughParent);
84+
$this->setConstraintsStaticFlag($resourceModel);
7785

7886
return parent::__construct(
7987
$query,
@@ -93,12 +101,18 @@ public function __construct(
93101
*/
94102
protected function newMorphOne(Builder $query, Model $parent, $type, $id, $localKey): MorphOne
95103
{
96-
return new class($query, $parent, $type, $id, $localKey) extends MorphOne {
104+
return new class ($query, $parent, $type, $id, $localKey, $this) extends MorphOne {
97105
use RelationCleverTrait;
98106

99-
public function __construct(Builder $query, Model $parent, string $type, string $id, string $localKey)
100-
{
101-
$this->setConstraintsStaticFlag($parent);
107+
public function __construct(
108+
Builder $query,
109+
Model $parent,
110+
string $type,
111+
string $id,
112+
string $localKey,
113+
BaseModel $resourceModel
114+
) {
115+
$this->setConstraintsStaticFlag($resourceModel);
102116

103117
return parent::__construct($query, $parent, $type, $id, $localKey);
104118
}
@@ -115,17 +129,18 @@ protected function newBelongsTo(
115129
$ownerKey,
116130
$relation
117131
): BelongsTo {
118-
return new class($query, $child, $foreignKey, $ownerKey, $relation) extends BelongsTo {
132+
return new class ($query, $child, $foreignKey, $ownerKey, $relation, $this) extends BelongsTo {
119133
use RelationCleverTrait;
120134

121135
public function __construct(
122136
Builder $query,
123137
Model $child,
124138
string $foreignKey,
125139
string $ownerKey,
126-
string $relation
140+
string $relation,
141+
BaseModel $resourceModel
127142
) {
128-
$this->setConstraintsStaticFlag($child);
143+
$this->setConstraintsStaticFlag($resourceModel);
129144

130145
return parent::__construct($query, $child, $foreignKey, $ownerKey, $relation);
131146
}
@@ -143,7 +158,7 @@ protected function newMorphTo(
143158
$type,
144159
$relation
145160
): MorphTo {
146-
return new class($query, $parent, $foreignKey, $ownerKey, $type, $relation) extends MorphTo {
161+
return new class ($query, $parent, $foreignKey, $ownerKey, $type, $relation, $this) extends MorphTo {
147162
use RelationCleverTrait;
148163

149164
public function __construct(
@@ -152,9 +167,10 @@ public function __construct(
152167
string $foreignKey,
153168
string $ownerKey,
154169
string $type,
155-
string $relation
170+
string $relation,
171+
BaseModel $resourceModel
156172
) {
157-
$this->setConstraintsStaticFlag($parent);
173+
$this->setConstraintsStaticFlag($resourceModel);
158174

159175
return parent::__construct($query, $parent, $foreignKey, $ownerKey, $type, $relation);
160176
}
@@ -166,12 +182,17 @@ public function __construct(
166182
*/
167183
protected function newHasMany(Builder $query, Model $parent, $foreignKey, $localKey): HasMany
168184
{
169-
return new class($query, $parent, $foreignKey, $localKey) extends HasMany {
185+
return new class ($query, $parent, $foreignKey, $localKey, $this) extends HasMany {
170186
use RelationCleverTrait;
171187

172-
public function __construct(Builder $query, Model $parent, string $foreignKey, string $localKey)
173-
{
174-
$this->setConstraintsStaticFlag($parent);
188+
public function __construct(
189+
Builder $query,
190+
Model $parent,
191+
string $foreignKey,
192+
string $localKey,
193+
BaseModel $resourceModel
194+
) {
195+
$this->setConstraintsStaticFlag($resourceModel);
175196

176197
return parent::__construct($query, $parent, $foreignKey, $localKey);
177198
}
@@ -190,8 +211,16 @@ protected function newHasManyThrough(
190211
$localKey,
191212
$secondLocalKey
192213
): HasManyThrough {
193-
return new class($query, $farParent, $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey) extends
194-
HasManyThrough {
214+
return new class (
215+
$query,
216+
$farParent,
217+
$throughParent,
218+
$firstKey,
219+
$secondKey,
220+
$localKey,
221+
$secondLocalKey,
222+
$this
223+
) extends HasManyThrough {
195224
use RelationCleverTrait;
196225

197226
public function __construct(
@@ -201,9 +230,10 @@ public function __construct(
201230
string $firstKey,
202231
string $secondKey,
203232
string $localKey,
204-
string $secondLocalKey
233+
string $secondLocalKey,
234+
BaseModel $resourceModel
205235
) {
206-
$this->setConstraintsStaticFlag($throughParent);
236+
$this->setConstraintsStaticFlag($resourceModel);
207237

208238
return parent::__construct(
209239
$query,
@@ -223,12 +253,18 @@ public function __construct(
223253
*/
224254
protected function newMorphMany(Builder $query, Model $parent, $type, $id, $localKey): MorphMany
225255
{
226-
return new class($query, $parent, $type, $id, $localKey) extends MorphMany {
256+
return new class ($query, $parent, $type, $id, $localKey, $this) extends MorphMany {
227257
use RelationCleverTrait;
228258

229-
public function __construct(Builder $query, Model $parent, string $type, string $id, string $localKey)
230-
{
231-
$this->setConstraintsStaticFlag($parent);
259+
public function __construct(
260+
Builder $query,
261+
Model $parent,
262+
string $type,
263+
string $id,
264+
string $localKey,
265+
BaseModel $resourceModel
266+
) {
267+
$this->setConstraintsStaticFlag($resourceModel);
232268

233269
return parent::__construct($query, $parent, $type, $id, $localKey);
234270
}
@@ -248,8 +284,17 @@ protected function newBelongsToMany(
248284
$relatedKey,
249285
$relationName = null
250286
): BelongsToMany {
251-
return new class($query, $parent, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName) extends
252-
BelongsToMany {
287+
return new class (
288+
$query,
289+
$parent,
290+
$table,
291+
$foreignPivotKey,
292+
$relatedPivotKey,
293+
$parentKey,
294+
$relatedKey,
295+
$this,
296+
$relationName
297+
) extends BelongsToMany {
253298
use RelationCleverTrait;
254299

255300
public function __construct(
@@ -260,9 +305,10 @@ public function __construct(
260305
string $relatedPivotKey,
261306
string $parentKey,
262307
string $relatedKey,
308+
BaseModel $resourceModel,
263309
?string $relationName = null
264310
) {
265-
$this->setConstraintsStaticFlag($parent);
311+
$this->setConstraintsStaticFlag($resourceModel);
266312

267313
return parent::__construct(
268314
$query,
@@ -293,7 +339,7 @@ protected function newMorphToMany(
293339
$relationName = null,
294340
$inverse = false
295341
): MorphToMany {
296-
return new class(
342+
return new class (
297343
$query,
298344
$parent,
299345
$name,
@@ -303,7 +349,8 @@ protected function newMorphToMany(
303349
$parentKey,
304350
$relatedKey,
305351
$relationName,
306-
$inverse
352+
$inverse,
353+
$this
307354
) extends MorphToMany {
308355
use RelationCleverTrait;
309356

@@ -317,9 +364,10 @@ public function __construct(
317364
string $parentKey,
318365
string $relatedKey,
319366
?string $relationName,
320-
bool $inverse
367+
bool $inverse,
368+
BaseModel $resourceModel
321369
) {
322-
$this->setConstraintsStaticFlag($parent);
370+
$this->setConstraintsStaticFlag($resourceModel);
323371

324372
return parent::__construct(
325373
$query,

0 commit comments

Comments
 (0)