Skip to content

Commit 221116f

Browse files
committed
Allow callables for config
1 parent f02937b commit 221116f

File tree

3 files changed

+64
-3
lines changed

3 files changed

+64
-3
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,31 @@ class Parent extends Model
126126
}
127127
}
128128
```
129+
130+
You may also pass a callable as the config value. This is useful if you want to disable this behavior on some requests. See example below.
131+
132+
## Laravel Nova
133+
134+
It's a good idea to disable setting the relationship on resolution for Nova requests. They tend to make a lot of queries and this can slow the page down (or result in 502 errors).
135+
136+
Here's an example implementation using a base model and adding config to filter out Nova requests.
137+
138+
```php
139+
abstract class Model extends EloquentModel
140+
{
141+
use HasManyWithInverse {
142+
hasManyWithInverse as originalHasManyWithInverse;
143+
}
144+
145+
public function hasManyWithInverse($related, $inverse, $foreignKey = null, $localKey = null, $config = [])
146+
{
147+
$config = array_merge(['setRelationOnResolution' => function () {
148+
if (request()->route() && in_array('nova', request()->route()->middleware())) {
149+
return false;
150+
}
151+
}], $config);
152+
153+
return $this->originalHasManyWithInverse($related, $inverse, $foreignKey, $localKey, $config);
154+
}
155+
}
156+
```

src/HasManyWithInverseRelationship.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function create(array $attributes = [])
3232
return tap($this->related->newInstance($attributes), function ($instance) {
3333
$this->setForeignAttributesForCreate($instance);
3434

35-
if ($this->config['setRelationOnCreation'] ?? true) {
35+
if ($this->config('setRelationOnCreation', true)) {
3636
$instance->setRelation($this->relationToParent, $this->getParent());
3737
}
3838

@@ -44,11 +44,23 @@ public function getResults()
4444
{
4545
$results = parent::getResults();
4646

47-
if ($this->config['setRelationOnResolution'] ?? true) {
47+
if ($this->config('setRelationOnResolution', true)) {
4848
$results->each->setRelation($this->relationToParent, $this->getParent());
4949
}
5050

5151
return $results;
5252
}
53+
54+
protected function config(string $key, $default)
55+
{
56+
if (! isset($this->config[$key])) {
57+
return $default;
58+
}
59+
60+
if (is_callable($this->config[$key])) {
61+
return $this->config[$key]();
62+
}
63+
64+
return $this->config[$key];
65+
}
5366
}
54-

tests/ConfigTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,25 @@ public function children()
4141

4242
$this->assertFalse($parent->children->first()->relationLoaded('parent'));
4343
}
44+
45+
/** @test */
46+
public function config_value_can_be_a_closure()
47+
{
48+
/** @var ParentModel $parent */
49+
$parent = (new class extends ParentModel {
50+
public function children()
51+
{
52+
return $this->hasManyWithInverse(ChildModel::class, 'parent', 'parent_id', null, [
53+
'setRelationOnResolution' => function () {
54+
return false;
55+
},
56+
]);
57+
}
58+
})::create([]);
59+
60+
/** @var ChildModel $child */
61+
$child = $parent->children()->create([]);
62+
63+
$this->assertFalse($parent->children->first()->relationLoaded('parent'));
64+
}
4465
}

0 commit comments

Comments
 (0)