Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function modelToResponse(Model $model, Resource $resource, array $request
$currentRequestArray = $relation === null ? $requestArray : collect($requestArray['includes'] ?? [])
->first(function ($include) use ($relation) {
return preg_match('/(?:\.\b)?'.$relation->relation.'\b/', $include['relation']);
});
}) ?? [];

return array_merge(
// toArray to take advantage of Laravel's logic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
use Lomkit\Rest\Tests\Support\Database\Factories\BelongsToManyRelationFactory;
use Lomkit\Rest\Tests\Support\Database\Factories\BelongsToRelationFactory;
use Lomkit\Rest\Tests\Support\Database\Factories\HasManyRelationFactory;
use Lomkit\Rest\Tests\Support\Database\Factories\HasManyThroughRelationFactory;
use Lomkit\Rest\Tests\Support\Database\Factories\HasOneOfManyRelationFactory;
use Lomkit\Rest\Tests\Support\Database\Factories\HasOneRelationFactory;
use Lomkit\Rest\Tests\Support\Database\Factories\ModelFactory;
use Lomkit\Rest\Tests\Support\Database\Factories\ModelWithFactory;
use Lomkit\Rest\Tests\Support\Models\BelongsToManyRelation;
use Lomkit\Rest\Tests\Support\Models\BelongsToRelation;
use Lomkit\Rest\Tests\Support\Models\HasManyRelation;
use Lomkit\Rest\Tests\Support\Models\HasManyThroughRelation;
use Lomkit\Rest\Tests\Support\Models\HasOneOfManyRelation;
use Lomkit\Rest\Tests\Support\Models\HasOneRelation;
use Lomkit\Rest\Tests\Support\Models\Model;
Expand All @@ -25,6 +27,7 @@
use Lomkit\Rest\Tests\Support\Rest\Resources\BelongsToManyResource;
use Lomkit\Rest\Tests\Support\Rest\Resources\BelongsToResource;
use Lomkit\Rest\Tests\Support\Rest\Resources\HasManyResource;
use Lomkit\Rest\Tests\Support\Rest\Resources\HasManyThroughResource;
use Lomkit\Rest\Tests\Support\Rest\Resources\HasOneOfManyResource;
use Lomkit\Rest\Tests\Support\Rest\Resources\HasOneResource;
use Lomkit\Rest\Tests\Support\Rest\Resources\ModelResource;
Expand Down Expand Up @@ -578,6 +581,69 @@ public function test_getting_a_list_of_resources_including_has_many_relation():
);
}

public function test_getting_a_list_of_resources_including_has_many_relation_with_eager_loading_relations(): void
{
$matchingModel = ModelFactory::new()
->createOne()->fresh();

$hasMany = HasManyRelationFactory::new()
->for($matchingModel)
->createOne();

HasManyThroughRelationFactory::new()
->for($hasMany)
->createOne();

Gate::policy(Model::class, GreenPolicy::class);
Gate::policy(HasManyRelation::class, GreenPolicy::class);
Gate::policy(HasManyThroughRelation::class, GreenPolicy::class);

$response = $this->post(
'/api/models/search',
[
'search' => [
'includes' => [
[
'relation' => 'hasManyRelationWithEagerLoadingRelation',
],
],
],
],
['Accept' => 'application/json']
);

$this->assertResourcePaginated(
$response,
[$matchingModel],
new ModelResource(),
[
[
'has_many_relation_with_eager_loading_relation' => $matchingModel->hasManyRelationWithEagerLoadingRelation()
->orderBy('id')
->get()
->map(function ($relation) {
$relation->has_many_through_relation = $relation->hasManyThroughRelation
->map(function ($relation) {
$relation->has_many_relation = $relation->hasManyRelation->only(
(new HasManyResource())->getFields(app()->make(RestRequest::class))
);

return $relation->only(array_merge(
(new HasManyThroughResource())->getFields(app()->make(RestRequest::class)),
['has_many_relation']
));
})->toArray();

return $relation->only(array_merge(
(new HasManyResource())->getFields(app()->make(RestRequest::class)),
['has_many_through_relation']
));
})->toArray(),
],
]
);
}

public function test_getting_a_list_of_resources_including_belongs_to_many_relation(): void
{
$matchingModel = ModelFactory::new()
Expand Down
5 changes: 5 additions & 0 deletions tests/Support/Models/HasManyRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ public function model()
{
return $this->belongsTo(\Lomkit\Rest\Tests\Support\Models\Model::class);
}

public function hasManyThroughRelation()
{
return $this->hasMany(HasManyThroughRelation::class, 'has_many_relation_id');
}
}
6 changes: 6 additions & 0 deletions tests/Support/Models/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ public function hasManyRelation()
return $this->hasMany(HasManyRelation::class, 'model_id');
}

public function hasManyRelationWithEagerLoadingRelation()
{
return $this->hasMany(HasManyRelation::class, 'model_id')
->with('hasManyThroughRelation.hasManyRelation');
}

public function hasOneOfManyRelation()
{
return $this->hasOne(HasOneOfManyRelation::class, 'model_id')->ofMany();
Expand Down
2 changes: 2 additions & 0 deletions tests/Support/Rest/Resources/HasManyResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Lomkit\Rest\Http\Requests\RestRequest;
use Lomkit\Rest\Http\Resource;
use Lomkit\Rest\Relations\BelongsTo;
use Lomkit\Rest\Relations\HasMany;
use Lomkit\Rest\Tests\Support\Models\HasManyRelation;

class HasManyResource extends Resource
Expand All @@ -17,6 +18,7 @@ public function relations(RestRequest $request): array
{
return [
BelongsTo::make('model', ModelResource::class),
HasMany::make('hasManyThroughRelation', HasManyThroughResource::class),
];
}

Expand Down
5 changes: 4 additions & 1 deletion tests/Support/Rest/Resources/HasManyThroughResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Lomkit\Rest\Concerns\Resource\DisableGates;
use Lomkit\Rest\Http\Requests\RestRequest;
use Lomkit\Rest\Http\Resource;
use Lomkit\Rest\Relations\BelongsTo;
use Lomkit\Rest\Tests\Support\Models\HasManyThroughRelation;

class HasManyThroughResource extends Resource
Expand All @@ -14,7 +15,9 @@ class HasManyThroughResource extends Resource

public function relations(RestRequest $request): array
{
return [];
return [
BelongsTo::make('hasManyRelation', HasManyResource::class),
];
}

public function fields(RestRequest $request): array
Expand Down
1 change: 1 addition & 0 deletions tests/Support/Rest/Resources/ModelResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public function relations(RestRequest $request): array
HasOneOfMany::make('hasOneOfManyRelation', HasOneOfManyResource::class),
BelongsTo::make('belongsToRelation', BelongsToResource::class),
HasMany::make('hasManyRelation', HasManyResource::class),
HasMany::make('hasManyRelationWithEagerLoadingRelation', HasManyResource::class),
BelongsToMany::make('belongsToManyRelation', BelongsToManyResource::class)
->withPivotRules([
'number' => 'numeric',
Expand Down