All features that are specific to Laravel applications are listed here.
In order for Laravel 9 Attributes to be recognized as model properties, they must be protected
methods annotated with the Attribute
Generic Types.
The first generic type is the getter return type, and the second is the setter argument type.
/** @return Attribute<string[], string[]> */
protected function scopes(): Attribute
{
return Attribute::make(
get: fn (?string $value) => is_null($value) ? [] : explode(' ', $value),
set: function(array $value) {
$set = array_unique($value);
sort($set);
return ['scopes' => implode(' ', $set)];
}
);
}
/** @return Attribute<bool, never> */
protected function isTrue(): Attribute
{
return Attribute::make(
get: fn (?string $value): bool => $value === null,
);
}
In order for Larastan to recognize Model relationships:
- the return type must be defined
- the method must be
public
- the relationship method argument must be a literal string (not a variable)
If the above conditions are not met, then adding the @return
docblock can help
/** @return BelongsTo<User, $this> */
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
/** @return HasMany<Post> */
public function posts(): HasMany
{
return $this->hasMany(Post::class);
}