Skip to content

Commit 6d4e248

Browse files
toitzisoyuka
andauthoredOct 4, 2024
fix(laravel): hiding/showing relationships (#6679)
* fix(laravel): fix hiding/showing relationships Check if is in visible or hidden (if exists) just like with regular proeprties Closes: #6678 Signed-off-by: Tobias Oitzinger <tobiasoitzinger@gmail.com> * test: use a WorkbenchTestCase --------- Signed-off-by: Tobias Oitzinger <tobiasoitzinger@gmail.com> Co-authored-by: soyuka <soyuka@users.noreply.github.com>
1 parent f839c93 commit 6d4e248

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed
 

‎src/Laravel/Eloquent/Metadata/ModelMetadata.php

+1
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ public function getRelations(Model $model): Collection
156156
|| $method->isAbstract()
157157
|| Model::class === $method->getDeclaringClass()->getName()
158158
|| $method->getNumberOfParameters() > 0
159+
|| $this->attributeIsHidden($method->getName(), $model)
159160
)
160161
->filter(function (\ReflectionMethod $method) {
161162
if ($method->getReturnType() instanceof \ReflectionNamedType
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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 ApiPlatform\Laravel\Tests\Eloquent\Metadata;
15+
16+
use ApiPlatform\Laravel\Eloquent\Metadata\ModelMetadata;
17+
use Illuminate\Database\Eloquent\Model;
18+
use Illuminate\Database\Eloquent\Relations\HasMany;
19+
use Illuminate\Foundation\Testing\RefreshDatabase;
20+
use Orchestra\Testbench\Concerns\WithWorkbench;
21+
use Orchestra\Testbench\TestCase;
22+
use Workbench\App\Models\Book;
23+
24+
/**
25+
* @author Tobias Oitzinger <tobiasoitzinger@gmail.com>
26+
*/
27+
final class ModelMetadataTest extends TestCase
28+
{
29+
use RefreshDatabase;
30+
use WithWorkbench;
31+
32+
public function testHiddenAttributesAreCorrectlyIdentified(): void
33+
{
34+
$model = new class extends Model {
35+
protected $hidden = ['secret'];
36+
37+
/**
38+
* @return HasMany<Book>
39+
*/
40+
public function secret(): HasMany
41+
{
42+
return $this->hasMany(Book::class);
43+
}
44+
};
45+
46+
$metadata = new ModelMetadata();
47+
$this->assertCount(0, $metadata->getRelations($model));
48+
}
49+
50+
public function testVisibleAttributesAreCorrectlyIdentified(): void
51+
{
52+
$model = new class extends Model {
53+
protected $visible = ['secret'];
54+
55+
/**
56+
* @return HasMany<Book>
57+
*/
58+
public function secret(): HasMany
59+
{
60+
return $this->hasMany(Book::class);
61+
}
62+
};
63+
64+
$metadata = new ModelMetadata();
65+
$this->assertCount(1, $metadata->getRelations($model));
66+
}
67+
68+
public function testAllAttributesVisibleByDefault(): void
69+
{
70+
$model = new class extends Model {
71+
/**
72+
* @return HasMany<Book>
73+
*/
74+
public function secret(): HasMany
75+
{
76+
return $this->hasMany(Book::class);
77+
}
78+
};
79+
80+
$metadata = new ModelMetadata();
81+
$this->assertCount(1, $metadata->getRelations($model));
82+
}
83+
}

0 commit comments

Comments
 (0)