Skip to content

Commit a1bfbbe

Browse files
committed
🐛 belongs to many relation wrong relation applied
1 parent cf13503 commit a1bfbbe

10 files changed

+108
-46
lines changed

src/Concerns/Resource/Relationable.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ public function relation($name)
2727
return $relation->relation === $relationName;
2828
});
2929

30-
if ($isSubRelation && Str::contains($nestedRelation = Str::after($name, '.'), '.')) {
30+
if ($isSubRelation) {
31+
$nestedRelation = Str::after($name, '.');
3132
return $relation->resource()->relation($nestedRelation);
3233
}
3334

src/Query/Traits/PerformSearch.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ public function filter($field, $operator, $value, $type = 'and', $nested = null)
8585

8686
// Here we assume the user has asked a relation filter
8787
if (Str::contains($field, '.')) {
88-
$relation = $this->resource->relation($field);
88+
$relation = $this->resource->relation(
89+
Str::beforeLast($field, '.')
90+
);
8991

9092
return $relation->filter($this->queryBuilder, $field, $operator, $value, $type, function ($query) use ($relation) {
9193
$relation->applySearchQuery($query);

src/helpers.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
*/
1111
function relation_without_pivot(string $relation)
1212
{
13-
return \Illuminate\Support\Str::contains($relation, '.pivot.') ?
14-
\Illuminate\Support\Str::replaceLast('pivot.', '', $relation) :
13+
return \Illuminate\Support\Str::contains($relation, '.pivot.') || \Illuminate\Support\Str::endsWith($relation, '.pivot') ?
14+
\Illuminate\Support\Str::replaceLast('.pivot', '', $relation) :
1515
$relation;
1616
}
1717
}

tests/Feature/Controllers/SearchIncludingRelationshipsOperationsTest.php

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
namespace Lomkit\Rest\Tests\Feature\Controllers;
44

5+
use Illuminate\Support\Arr;
56
use Illuminate\Support\Facades\Gate;
67
use Lomkit\Rest\Http\Requests\RestRequest;
8+
use Lomkit\Rest\Relations\Relation;
79
use Lomkit\Rest\Tests\Feature\TestCase;
810
use Lomkit\Rest\Tests\Support\Database\Factories\BelongsToManyRelationFactory;
911
use Lomkit\Rest\Tests\Support\Database\Factories\BelongsToRelationFactory;
@@ -20,6 +22,7 @@
2022
use Lomkit\Rest\Tests\Support\Models\Model;
2123
use Lomkit\Rest\Tests\Support\Models\ModelWith;
2224
use Lomkit\Rest\Tests\Support\Policies\GreenPolicy;
25+
use Lomkit\Rest\Tests\Support\Rest\Resources\BelongsToManyQueryChangesResource;
2326
use Lomkit\Rest\Tests\Support\Rest\Resources\BelongsToManyResource;
2427
use Lomkit\Rest\Tests\Support\Rest\Resources\BelongsToResource;
2528
use Lomkit\Rest\Tests\Support\Rest\Resources\HasManyResource;
@@ -261,15 +264,22 @@ public function test_getting_a_list_of_resources_including_belongs_to_has_many_r
261264

262265
public function test_getting_a_list_of_resources_including_distant_relation_with_intermediary_search_query_condition(): void
263266
{
264-
$belongsToMany = BelongsToManyRelationFactory::new()->create();
265-
$matchingModel = ModelFactory::new()
267+
$matchingModel = ModelFactory::new()->create(
268+
['number' => 1]
269+
)->fresh();
270+
271+
$belongsToMany = BelongsToManyRelationFactory::new()
272+
->for($matchingModel)
273+
->create();
274+
275+
$matchingModel2 = ModelFactory::new()
266276
->afterCreating(function (Model $model) use ($belongsToMany) {
267277
$model->belongsToManyQueryChangesRelation()
268278
->attach($belongsToMany);
269279
})
270280
->create()->fresh();
271281

272-
$matchingModel2 = ModelFactory::new()->create()->fresh();
282+
273283

274284
Gate::policy(Model::class, GreenPolicy::class);
275285
Gate::policy(BelongsToManyRelation::class, GreenPolicy::class);
@@ -279,37 +289,48 @@ public function test_getting_a_list_of_resources_including_distant_relation_with
279289
[
280290
'search' => [
281291
'includes' => [
282-
['relation' => 'belongsToManyQueryChangesRelation.model'],
292+
['relation' => 'belongsToManyRelation.model'],
283293
],
284294
],
285295
],
286296
['Accept' => 'application/json']
287297
);
288298

289-
dd($response->getContent());
290299

291-
$matchingModelBelongsToRelation = $matchingModel->belongsToRelation;
300+
$matchingModelBelongsToManyQueryChangesRelations = $matchingModel2->belongsToManyQueryChangesRelation;
292301

293302
$this->assertResourcePaginated(
294303
$response,
295304
[$matchingModel, $matchingModel2],
296305
new ModelResource(),
297306
[
298307
[
299-
'belongs_to_relation' => array_merge(
300-
$matchingModelBelongsToRelation
301-
->only((new BelongsToResource())->getFields(app()->make(RestRequest::class))),
302-
[
303-
'models' => $matchingModelBelongsToRelation->models
304-
->map(function ($model) {
305-
return $model->only((new ModelResource())->getFields(app()->make(RestRequest::class)));
306-
})
307-
->toArray(),
308-
]
309-
),
308+
'belongs_to_many_relation' => [],
310309
],
311310
[
312-
'belongs_to_relation' => null,
311+
'belongs_to_many_relation' =>
312+
$matchingModelBelongsToManyQueryChangesRelations
313+
->map(function (BelongsToManyRelation $belongsToManyRelation) {
314+
return array_merge(
315+
$belongsToManyRelation
316+
->only((new BelongsToManyResource)->getFields(app()->make(RestRequest::class))),
317+
[
318+
'model' => null,
319+
'belongs_to_many_pivot' => Arr::only(
320+
$belongsToManyRelation
321+
->belongs_to_many_pivot
322+
->toArray(),
323+
Arr::first(
324+
(new ModelResource())->getRelations(app()->make(RestRequest::class)),
325+
function (Relation $relation) {
326+
return $relation->relation === 'belongsToManyRelation';
327+
}
328+
)->getPivotFields()
329+
)
330+
]
331+
);
332+
})
333+
->toArray(),
313334
],
314335
]
315336
);

tests/Support/Database/migrations/2023_02_00_000000_create_belongs_to_many_relations_table.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ public function up()
1515
Schema::create('belongs_to_many_relations', function (Blueprint $table) {
1616
$table->id();
1717
$table->integer('number')->default(0);
18-
$table->foreignIdFor(\Lomkit\Rest\Tests\Support\Models\BelongsToManyRelation::class)->nullable()->constrained();
1918
$table->timestamps();
2019
});
2120
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class() extends Migration {
8+
/**
9+
* Run the migrations.
10+
*
11+
* @return void
12+
*/
13+
public function up()
14+
{
15+
Schema::table('belongs_to_many_relations', function (Blueprint $table) {
16+
$table->foreignIdFor(\Lomkit\Rest\Tests\Support\Models\Model::class)->nullable()->constrained();
17+
});
18+
}
19+
};

tests/Support/Rest/Resources/BelongsToManyQueryChangesResource.php

Lines changed: 0 additions & 17 deletions
This file was deleted.

tests/Support/Rest/Resources/BelongsToManyResource.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Lomkit\Rest\Concerns\Resource\DisableGates;
66
use Lomkit\Rest\Http\Requests\RestRequest;
77
use Lomkit\Rest\Http\Resource;
8+
use Lomkit\Rest\Relations\BelongsTo;
89
use Lomkit\Rest\Tests\Support\Models\BelongsToManyRelation;
910

1011
class BelongsToManyResource extends Resource
@@ -15,7 +16,9 @@ class BelongsToManyResource extends Resource
1516

1617
public function relations(RestRequest $request): array
1718
{
18-
return [];
19+
return [
20+
BelongsTo::make('model', ModelQueryChangedResource::class)
21+
];
1922
}
2023

2124
public function fields(RestRequest $request): array
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Lomkit\Rest\Tests\Support\Rest\Resources;
4+
5+
use Lomkit\Rest\Concerns\Resource\DisableGates;
6+
use Lomkit\Rest\Http\Requests\RestRequest;
7+
use Lomkit\Rest\Http\Resource;
8+
use Lomkit\Rest\Relations\BelongsTo;
9+
use Lomkit\Rest\Relations\BelongsToMany;
10+
use Lomkit\Rest\Relations\HasMany;
11+
use Lomkit\Rest\Relations\HasManyThrough;
12+
use Lomkit\Rest\Relations\HasOne;
13+
use Lomkit\Rest\Relations\HasOneOfMany;
14+
use Lomkit\Rest\Relations\HasOneThrough;
15+
use Lomkit\Rest\Relations\MorphedByMany;
16+
use Lomkit\Rest\Relations\MorphMany;
17+
use Lomkit\Rest\Relations\MorphOne;
18+
use Lomkit\Rest\Relations\MorphOneOfMany;
19+
use Lomkit\Rest\Relations\MorphTo;
20+
use Lomkit\Rest\Relations\MorphToMany;
21+
use Lomkit\Rest\Tests\Support\Models\Model;
22+
use Lomkit\Rest\Tests\Support\Rest\Actions\BatchableModifyNumberAction;
23+
use Lomkit\Rest\Tests\Support\Rest\Actions\ModifyNumberAction;
24+
use Lomkit\Rest\Tests\Support\Rest\Actions\QueueableModifyNumberAction;
25+
use Lomkit\Rest\Tests\Support\Rest\Actions\StandaloneModifyNumberAction;
26+
use Lomkit\Rest\Tests\Support\Rest\Actions\WithMetaModifyNumberAction;
27+
use Lomkit\Rest\Tests\Support\Rest\Instructions\NumberedInstruction;
28+
29+
class ModelQueryChangedResource extends ModelResource
30+
{
31+
public function searchQuery(\Lomkit\Rest\Http\Requests\RestRequest $request, \Illuminate\Contracts\Database\Eloquent\Builder $query)
32+
{
33+
$query = parent::searchQuery($request, $query);
34+
35+
$query->where('number', 10000);
36+
37+
return $query;
38+
}
39+
}

tests/Support/Rest/Resources/ModelResource.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,6 @@ public function relations(RestRequest $request): array
5858
'number' => 'numeric',
5959
])
6060
->withPivotFields(['created_at', 'number']),
61-
BelongsToMany::make('belongsToManyQueryChangesRelation', BelongsToManyQueryChangesResource::class)
62-
->withPivotRules([
63-
'number' => 'numeric',
64-
])
65-
->withPivotFields(['created_at', 'number']),
6661

6762
// Through relationships
6863
HasOneThrough::make('hasOneThroughRelation', HasOneThroughResource::class),

0 commit comments

Comments
 (0)