Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Laravel] Fix virtual fields metadata #6633

Draft
wants to merge 3 commits into
base: 4.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ public function create(string $resourceClass, array $options = []): PropertyName
*/
$properties = [];

// When it's an Eloquent model we read attributes from database (@see ShowModelCommand)
/*
* When it's an Eloquent model we read attributes from database
* @see \Illuminate\Database\Console\ShowModelCommand
*/
foreach ($this->modelMetadata->getAttributes($model) as $property) {
if (!$property['primary'] && $property['hidden']) {
continue;
Expand Down
6 changes: 4 additions & 2 deletions src/Laravel/Eloquent/Metadata/ModelMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ private function isColumnPrimaryKey(array $indexes, string $column): bool
public function getVirtualAttributes(Model $model, array $columns): Collection
{
$class = new \ReflectionClass($model);
$columns = collect($columns);

return collect($class->getMethods())
->reject(
Expand All @@ -126,18 +127,19 @@ public function getVirtualAttributes(Model $model, array $columns): Collection

return [];
})
->reject(fn ($cast, $name) => collect($columns)->contains('name', $name))
->reject(fn ($cast, $name) => $columns->contains('name', $name))
->map(fn ($cast, $name) => [
'name' => $name,
'type' => null,
'increments' => false,
'nullable' => null,
'nullable' => true,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The nullable field must be a boolean, as it is given to the new Type() here:

$type = match ($builtinType) {
'double', 'real' => new Type(Type::BUILTIN_TYPE_FLOAT, $p['nullable']),
'datetime', 'date', 'timestamp' => new Type(Type::BUILTIN_TYPE_OBJECT, $p['nullable'], \DateTime::class),
'immutable_datetime', 'immutable_date' => new Type(Type::BUILTIN_TYPE_OBJECT, $p['nullable'], \DateTimeImmutable::class),
'collection', 'encrypted:collection' => new Type(Type::BUILTIN_TYPE_ITERABLE, $p['nullable'], Collection::class, true),
'encrypted:array' => new Type(Type::BUILTIN_TYPE_ARRAY, $p['nullable']),
'encrypted:object' => new Type(Type::BUILTIN_TYPE_OBJECT, $p['nullable']),
default => new Type(\in_array($builtinType, Type::$builtinTypes, true) ? $builtinType : Type::BUILTIN_TYPE_STRING, $p['nullable']),
};

'default' => null,
'unique' => null,
'fillable' => $model->isFillable($name),
'hidden' => $this->attributeIsHidden($name, $model),
'appended' => $model->hasAppended($name),
'cast' => $cast,
'primary' => $name === $model->getKeyName(),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The primary attribute is accessed for every fields here:

foreach ($this->modelMetadata->getAttributes($model) as $property) {
if (!$property['primary'] && $property['hidden']) {

In MongoDB, "id" ends as a "virtual field" because we have an accessor for it: https://github.com/mongodb/laravel-mongodb/blob/5.0.2/src/Eloquent/DocumentModel.php#L78-L94
Maybe the metadata should be merged when a field is defined both in database schema and as a virtual field.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might also want to do this on line 83 in the same class and remove the isColumnPrimaryKey method all along, imho your solution is cleaner.

])
->values();
}
Expand Down
9 changes: 8 additions & 1 deletion src/Laravel/workbench/app/Models/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Str;
use Workbench\App\Http\Requests\BookFormRequest;

#[ApiResource(
Expand Down Expand Up @@ -63,11 +64,17 @@ class Book extends Model
use HasFactory;
use HasUlids;

protected $visible = ['name', 'author', 'isbn', 'publication_date'];
protected $visible = ['name', 'slug', 'author', 'isbn', 'publication_date'];
protected $fillable = ['name'];

public function author(): BelongsTo
{
return $this->belongsTo(Author::class);
}

// Virtual field
public function getSlugAttribute(): string
{
return Str::slug($this->name);
}
}
Loading