Skip to content

Latest commit

 

History

History
59 lines (47 loc) · 1.46 KB

features.md

File metadata and controls

59 lines (47 loc) · 1.46 KB

Features

All features that are specific to Laravel applications are listed here.

Laravel 9 Attributes

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.

Examples

/** @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,
	);
}

Model Relationships

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);
}