Skip to content

Commit 119ed64

Browse files
committed
fix: amend eager load iterator incorrectly filtering some paths
Closes #39
1 parent 18559d9 commit 119ed64

File tree

5 files changed

+27
-5
lines changed

5 files changed

+27
-5
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ All notable changes to this project will be documented in this file. This projec
99

1010
- [#38](https://github.com/laravel-json-api/eloquent/pull/38) Added `WhereAll` and `WhereAny` filters.
1111

12+
### Fixed
13+
14+
- [#39](https://github.com/laravel-json-api/eloquent/issues/39) Fixed a bug in the eager loader iterator where include
15+
paths starting with the same word were incorrectly removed. E.g. `car` and `carOwner` would result in just `carOwner`.
16+
1217
## [4.2.0] - 2024-08-26
1318

1419
### Added

src/QueryBuilder/EagerLoading/EagerLoadIterator.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
*/
2626
class EagerLoadIterator implements IteratorAggregate
2727
{
28-
2928
/**
3029
* @var Schema
3130
*/
@@ -70,11 +69,13 @@ public function __construct(Schema $schema, IncludePaths $paths)
7069
*/
7170
public function collect(): Collection
7271
{
73-
$values = collect($this);
72+
$values = Collection::make($this);
7473

75-
return $values->reject(
76-
fn($path) => $values->contains(fn($check) => $path !== $check && Str::startsWith($check, $path))
77-
)->sort()->values();
74+
return $values
75+
->reject(static fn(string $path) => $values
76+
->contains(fn(string $check) => $path !== $check && Str::startsWith($check, $path . '.')))
77+
->sort()
78+
->values();
7879
}
7980

8081
/**

tests/app/Models/Mechanic.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Illuminate\Database\Eloquent\Factories\HasFactory;
1515
use Illuminate\Database\Eloquent\Model;
16+
use Illuminate\Database\Eloquent\Relations\HasOne;
1617
use Illuminate\Database\Eloquent\Relations\HasOneThrough;
1718

1819
class Mechanic extends Model
@@ -25,6 +26,14 @@ class Mechanic extends Model
2526
*/
2627
protected $fillable = ['name'];
2728

29+
/**
30+
* @return HasOne
31+
*/
32+
public function car(): HasOne
33+
{
34+
return $this->hasOne(Car::class);
35+
}
36+
2837
/**
2938
* @return HasOneThrough
3039
*/

tests/app/Schemas/MechanicSchema.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use LaravelJsonApi\Eloquent\Contracts\Paginator;
1616
use LaravelJsonApi\Eloquent\Fields\DateTime;
1717
use LaravelJsonApi\Eloquent\Fields\ID;
18+
use LaravelJsonApi\Eloquent\Fields\Relations\HasOne;
1819
use LaravelJsonApi\Eloquent\Fields\Relations\HasOneThrough;
1920
use LaravelJsonApi\Eloquent\Fields\Str;
2021
use LaravelJsonApi\Eloquent\Filters\WhereIdIn;
@@ -39,6 +40,7 @@ public function fields(): array
3940
ID::make(),
4041
DateTime::make('createdAt')->readOnly(),
4142
Str::make('name'),
43+
HasOne::make('car'),
4244
HasOneThrough::make('carOwner'),
4345
DateTime::make('updatedAt')->readOnly(),
4446
];

tests/lib/Acceptance/EagerLoading/EagerLoaderTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ public static function includePathsProvider(): array
6262
'profile', // auto included for users
6363
],
6464
],
65+
'mechanic' => [
66+
'mechanics',
67+
'car,carOwner,carOwner.car,carOwner.car.mechanic,car.mechanic',
68+
['car.mechanic', 'carOwner.car.mechanic'],
69+
],
6570
];
6671
}
6772

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy