Skip to content

Commit 85f198a

Browse files
committed
fix(laravel): filter should query correct property
fixes #7370
1 parent 1a44dcf commit 85f198a

File tree

5 files changed

+122
-3
lines changed

5 files changed

+122
-3
lines changed

src/Laravel/Eloquent/Extension/FilterQueryExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function apply(Builder $builder, array $uriVariables, Operation $operatio
6060

6161
$filter = $filterId instanceof FilterInterface ? $filterId : ($this->filterLocator->has($filterId) ? $this->filterLocator->get($filterId) : null);
6262
if ($filter instanceof FilterInterface) {
63-
$builder = $filter->apply($builder, $values, $parameter, $context + ($parameter->getFilterContext() ?? []));
63+
$builder = $filter->apply($builder, $values, $parameter->withKey($parameter->getExtraProperties()['_query_property'] ?? $parameter->getKey()), $context + ($parameter->getFilterContext() ?? []));
6464
}
6565
}
6666

src/Laravel/Tests/EloquentTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -625,11 +625,11 @@ public function testIriIsNotDenormalizedBeforeFormRequestValidation(): void
625625
'/api/slots',
626626
[
627627
'name' => 'Morning Slot',
628-
'area' => '/api/areas/'.$area->id,
628+
'area' => '/api/areas/'.$area->id, // @phpstan-ignore-line
629629
],
630630
['accept' => 'application/ld+json', 'content-type' => 'application/ld+json']
631631
)->assertStatus(201);
632632

633-
$this->assertSame(StoreSlotRequest::$receivedArea->name, $area->name);
633+
$this->assertSame(StoreSlotRequest::$receivedArea->name, $area->name); // @phpstan-ignore-line
634634
}
635635
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Workbench\App\Models;
15+
16+
use ApiPlatform\Laravel\Eloquent\Filter\OrderFilter;
17+
use ApiPlatform\Metadata\ApiResource;
18+
use ApiPlatform\Metadata\Get;
19+
use ApiPlatform\Metadata\GetCollection;
20+
use ApiPlatform\Metadata\QueryParameter;
21+
use Illuminate\Database\Eloquent\Factories\HasFactory;
22+
use Illuminate\Database\Eloquent\Model;
23+
24+
#[ApiResource(
25+
operations: [
26+
new Get(),
27+
new GetCollection(),
28+
]
29+
)]
30+
#[QueryParameter(key: 'sort[:property]', filter: OrderFilter::class, properties: ['isActive'])]
31+
class ActiveBook extends Model
32+
{
33+
use HasFactory;
34+
35+
protected $table = 'active_books';
36+
37+
protected $fillable = [
38+
'name',
39+
'is_active',
40+
];
41+
42+
protected $casts = [
43+
'is_active' => 'boolean',
44+
];
45+
46+
public function getIsActiveAttribute(bool $value): bool
47+
{
48+
return $value;
49+
}
50+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Workbench\Database\Factories;
15+
16+
use Illuminate\Database\Eloquent\Factories\Factory;
17+
use Workbench\App\Models\ActiveBook;
18+
19+
class ActiveBookFactory extends Factory
20+
{
21+
protected $model = ActiveBook::class;
22+
23+
public function definition(): array
24+
{
25+
return [
26+
'name' => $this->faker->sentence(),
27+
'is_active' => $this->faker->boolean(),
28+
];
29+
}
30+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
use Illuminate\Database\Migrations\Migration;
15+
use Illuminate\Database\Schema\Blueprint;
16+
use Illuminate\Support\Facades\Schema;
17+
18+
return new class extends Migration {
19+
/**
20+
* Run the migrations.
21+
*/
22+
public function up(): void
23+
{
24+
Schema::create('active_books', function (Blueprint $table): void {
25+
$table->id();
26+
$table->string('name');
27+
$table->boolean('is_active');
28+
$table->timestamps();
29+
});
30+
}
31+
32+
/**
33+
* Reverse the migrations.
34+
*/
35+
public function down(): void
36+
{
37+
Schema::dropIfExists('active_books');
38+
}
39+
};

0 commit comments

Comments
 (0)