Skip to content

Commit 2b4937a

Browse files
authoredSep 26, 2024··
fix(laravel): eloquent accessors (#6668)
1 parent a259d46 commit 2b4937a

File tree

7 files changed

+126
-2
lines changed

7 files changed

+126
-2
lines changed
 

‎src/Laravel/Eloquent/Metadata/Factory/Property/EloquentPropertyMetadataFactory.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function create(string $resourceClass, string $property, array $options =
7777
'collection', 'encrypted:collection' => new Type(Type::BUILTIN_TYPE_ITERABLE, $p['nullable'], Collection::class, true),
7878
'encrypted:array' => new Type(Type::BUILTIN_TYPE_ARRAY, $p['nullable']),
7979
'encrypted:object' => new Type(Type::BUILTIN_TYPE_OBJECT, $p['nullable']),
80-
default => new Type(\in_array($builtinType, Type::$builtinTypes, true) ? $builtinType : Type::BUILTIN_TYPE_STRING, $p['nullable']),
80+
default => new Type(\in_array($builtinType, Type::$builtinTypes, true) ? $builtinType : Type::BUILTIN_TYPE_STRING, $p['nullable'] ?? true),
8181
};
8282

8383
return $propertyMetadata

‎src/Laravel/Eloquent/Metadata/Factory/Property/EloquentPropertyNameCollectionMetadataFactory.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function create(string $resourceClass, array $options = []): PropertyName
5353

5454
// When it's an Eloquent model we read attributes from database (@see ShowModelCommand)
5555
foreach ($this->modelMetadata->getAttributes($model) as $property) {
56-
if (!$property['primary'] && $property['hidden']) {
56+
if (!($property['primary'] ?? null) && $property['hidden']) {
5757
continue;
5858
}
5959

‎src/Laravel/Tests/EloquentTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -368,4 +368,10 @@ public function testWrongOrderFilter(): void
368368
$res = $this->get('/api/authors?order[name]=something', ['Accept' => ['application/ld+json']]);
369369
$this->assertEquals($res->getStatusCode(), 422);
370370
}
371+
372+
public function testWithAccessor(): void
373+
{
374+
$res = $this->get('/api/with_accessors/1', ['Accept' => ['application/ld+json']]);
375+
$this->assertArraySubset(['name' => 'test'], $res->json());
376+
}
371377
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
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\Metadata\ApiResource;
17+
use Illuminate\Database\Eloquent\Casts\Attribute;
18+
use Illuminate\Database\Eloquent\Factories\HasFactory;
19+
use Illuminate\Database\Eloquent\Model;
20+
21+
#[ApiResource]
22+
class WithAccessor extends Model
23+
{
24+
use HasFactory;
25+
26+
protected $hidden = ['created_at', 'updated_at', 'id'];
27+
28+
protected function name(): Attribute
29+
{
30+
return Attribute::make(
31+
get: fn (string $value) => 'test',
32+
);
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
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\WithAccessor;
18+
19+
/**
20+
* @template TModel of \Workbench\App\Models\WithAccessor
21+
*
22+
* @extends \Illuminate\Database\Eloquent\Factories\Factory<TModel>
23+
*/
24+
class WithAccessorFactory extends Factory
25+
{
26+
/**
27+
* The name of the factory's corresponding model.
28+
*
29+
* @var class-string<TModel>
30+
*/
31+
protected $model = WithAccessor::class;
32+
33+
/**
34+
* Define the model's default state.
35+
*
36+
* @return array<string, mixed>
37+
*/
38+
public function definition(): array
39+
{
40+
return [
41+
'name' => strtolower(fake()->name()),
42+
];
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
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('with_accessors', function (Blueprint $table): void {
25+
$table->id();
26+
$table->string('name');
27+
$table->timestamps();
28+
});
29+
}
30+
31+
/**
32+
* Reverse the migrations.
33+
*/
34+
public function down(): void
35+
{
36+
Schema::dropIfExists('with_accessors');
37+
}
38+
};

‎src/Laravel/workbench/database/seeders/DatabaseSeeder.php

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Workbench\Database\Factories\PostFactory;
2121
use Workbench\Database\Factories\SluggableFactory;
2222
use Workbench\Database\Factories\UserFactory;
23+
use Workbench\Database\Factories\WithAccessorFactory;
2324

2425
class DatabaseSeeder extends Seeder
2526
{
@@ -32,5 +33,6 @@ public function run(): void
3233
PostFactory::new()->has(CommentFactory::new()->count(10))->count(10)->create();
3334
SluggableFactory::new()->count(10)->create();
3435
UserFactory::new()->create();
36+
WithAccessorFactory::new()->create();
3537
}
3638
}

0 commit comments

Comments
 (0)
Please sign in to comment.