Skip to content

Commit 4ee209e

Browse files
authored
fix(laravel): visible and hidden fields support (#6538)
* fix(laravel): visible and hidden fields support * fix test * fix tests * fix PHPStan
1 parent c315666 commit 4ee209e

File tree

6 files changed

+25
-5
lines changed

6 files changed

+25
-5
lines changed

src/Laravel/Eloquent/Metadata/ModelMetadata.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,12 +264,12 @@ private function getColumnDefault(array $column, Model $model): mixed
264264
*/
265265
private function attributeIsHidden(string $attribute, Model $model): bool
266266
{
267-
if (\count($model->getHidden()) > 0) {
268-
return \in_array($attribute, $model->getHidden(), true);
267+
if ($visible = $model->getVisible()) {
268+
return !\in_array($attribute, $visible, true);
269269
}
270270

271-
if (\count($model->getVisible()) > 0) {
272-
return !\in_array($attribute, $model->getVisible(), true);
271+
if ($hidden = $model->getHidden()) {
272+
return \in_array($attribute, $hidden, true);
273273
}
274274

275275
return false;

src/Laravel/Tests/JsonLdTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,4 +275,20 @@ public function testApiDocsRegex(): void
275275
$response = $this->get('/api/notexists', ['accept' => 'application/ld+json']);
276276
$response->assertNotFound();
277277
}
278+
279+
public function testHidden(): void
280+
{
281+
$response = $this->get('/api/posts/1/comments/1', ['accept' => 'application/ld+json']);
282+
$response->assertStatus(200);
283+
$response->assertHeader('content-type', 'application/ld+json; charset=utf-8');
284+
$response->assertJsonMissingPath('internalNote');
285+
}
286+
287+
public function testVisible(): void
288+
{
289+
$response = $this->get('/api/books', ['accept' => 'application/ld+json']);
290+
$response->assertStatus(200);
291+
$response->assertHeader('content-type', 'application/ld+json; charset=utf-8');
292+
$this->assertStringNotContainsString('internalNote', (string) $response->getContent());
293+
}
278294
}

src/Laravel/workbench/app/Models/Comment.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class Comment extends Model
5050
{
5151
use HasFactory;
5252

53-
protected $visible = ['text', 'post'];
53+
protected $hidden = ['internal_note'];
5454

5555
public function post(): BelongsTo
5656
{

src/Laravel/workbench/database/factories/BookFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function definition(): array
3737
'author_id' => AuthorFactory::new(),
3838
'isbn' => fake()->isbn13(),
3939
'publication_date' => fake()->optional()->date(),
40+
'internal_note' => fake()->text(),
4041
];
4142
}
4243
}

src/Laravel/workbench/database/factories/CommentFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public function definition(): array
4040
return [
4141
'post_id' => PostFactory::new(),
4242
'text' => fake()->text(),
43+
'internal_note' => fake()->optional()->text(),
4344
];
4445
}
4546
}

src/Laravel/workbench/database/migrations/2023_07_15_231244_create_book_table.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public function up(): void
3232
$table->string('name');
3333
$table->string('isbn');
3434
$table->date('publication_date')->nullable();
35+
$table->text('internal_note')->nullable();
3536
$table->integer('author_id')->unsigned();
3637
$table->foreign('author_id')->references('id')->on('authors');
3738
$table->timestamps();
@@ -53,6 +54,7 @@ public function up(): void
5354
Schema::create('comments', function (Blueprint $table): void {
5455
$table->id();
5556
$table->text('text');
57+
$table->text('internal_note')->nullable();
5658
$table->integer('post_id')->unsigned();
5759
$table->foreign('post_id')->references('id')->on('posts');
5860
$table->timestamps();

0 commit comments

Comments
 (0)