Skip to content

Commit a3a9660

Browse files
[6.x] Eager Loading Improvements (#418)
* Eager loading should be its own setting During my v6 refactoring, I had merged the "eloquent relationship discovery" stuff with the eager loading relationships setting. * `with` should still be an option * Swap the method used for eager loading
1 parent 3eed74a commit a3a9660

File tree

5 files changed

+38
-27
lines changed

5 files changed

+38
-27
lines changed

docs/upgrade-guides/v5-x-to-v6-0.md

-15
Original file line numberDiff line numberDiff line change
@@ -85,21 +85,6 @@ If you were using the Table mode on any of your Has Many fields, you should swit
8585
* `mode: table`
8686
* `'mode' => 'table'`
8787

88-
### Low: Changes to overriding eager loaded relationships.
89-
If you were previously overriding the "eager loaded relationships" in your resource's config array, you should change the key from `with` to `relationships`:
90-
91-
```php
92-
\App\Models\Product::class => [
93-
// ...
94-
95-
// Previously...
96-
'with' => ['tags', 'manufacturer'],
97-
98-
// Now...
99-
'relationships' => ['tags', 'manufacturer'],
100-
],
101-
```
102-
10388
## Previous upgrade guides
10489

10590
- [v3.x to v4.0](/upgrade-guides/v3-x-to-v4-0)

src/GraphQL/ResourceIndexQuery.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function resolve($root, $args)
3939
{
4040
$query = $this->resource->model()
4141
->newQuery()
42-
->with($this->resource->eloquentRelationships()->values()->all());
42+
->with($this->resource->eagerLoadingRelationships());
4343

4444
$this->filterQuery($query, $args['filter'] ?? []);
4545
$this->sortQuery($query, $args['sort'] ?? []);

src/Http/Controllers/CP/ResourceListingController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function index(FilteredRequest $request, Resource $resource)
2525
$sortDirection = $request->input('order', $resource->orderByDirection());
2626

2727
$query = $resource->model()
28-
->with($resource->eloquentRelationships()->values()->all())
28+
->with($resource->eagerLoadingRelationships())
2929
->orderBy($sortField, $sortDirection);
3030

3131
$query->when($query->hasNamedScope('runwayListing'), fn ($query) => $query->runwayListing());

src/Resource.php

+13-5
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,10 @@ public function titleField()
132132
}
133133

134134
/**
135-
* Automatically maps Eloquent relationships to their respective blueprint fields.
135+
* Maps Eloquent relationships to their respective blueprint fields.
136136
*/
137137
public function eloquentRelationships(): Collection
138138
{
139-
if ($eloquentRelationships = $this->config->get('relationships')) {
140-
return collect($eloquentRelationships);
141-
}
142-
143139
return $this->blueprint()->fields()->all()
144140
->filter(function (Field $field) {
145141
return $field->fieldtype() instanceof BelongsToFieldtype
@@ -172,6 +168,18 @@ public function eloquentRelationships(): Collection
172168
->filter(fn ($eloquentRelationship) => method_exists($this->model(), $eloquentRelationship));
173169
}
174170

171+
/**
172+
* Defines the relationships which should be eager loaded when querying the model.
173+
*/
174+
public function eagerLoadingRelationships(): array
175+
{
176+
if ($eagerLoadingRelationships = $this->config->get('with')) {
177+
return $eagerLoadingRelationships;
178+
}
179+
180+
return $this->eloquentRelationships()->values()->toArray();
181+
}
182+
175183
public function listableColumns(): Collection
176184
{
177185
return $this->blueprint()->fields()->all()

tests/ResourceTest.php

+23-5
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,36 @@ public function can_get_eloquent_relationships_for_runway_uri_routing()
5555
}
5656

5757
/** @test */
58-
public function can_get_eloquent_relationships_as_defined_in_config()
58+
public function can_get_eager_loading_relationships()
5959
{
60-
Config::set('runway.resources.StatamicRadPack\Runway\Tests\Fixtures\Models\Post.relationships', ['author']);
60+
Runway::discoverResources();
61+
62+
$resource = Runway::findResource('post');
63+
64+
$eagerLoadingRelationships = $resource->eagerLoadingRelationships();
65+
66+
$this->assertEquals([
67+
'author',
68+
'runwayUri',
69+
], $eagerLoadingRelationships);
70+
}
71+
72+
/** @test */
73+
public function can_get_eager_loading_relationships_from_config()
74+
{
75+
Config::set('runway.resources.StatamicRadPack\Runway\Tests\Fixtures\Models\Post.with', [
76+
'author',
77+
]);
6178

6279
Runway::discoverResources();
6380

6481
$resource = Runway::findResource('post');
6582

66-
$eloquentRelationships = $resource->eloquentRelationships();
83+
$eagerLoadingRelationships = $resource->eagerLoadingRelationships();
6784

68-
$this->assertContains('author', $eloquentRelationships->toArray());
69-
$this->assertNotContains('runwayUri', $eloquentRelationships->toArray());
85+
$this->assertEquals([
86+
'author',
87+
], $eagerLoadingRelationships);
7088
}
7189

7290
/** @test */

0 commit comments

Comments
 (0)