Skip to content

Commit

Permalink
docs: document model relationships (#1905)
Browse files Browse the repository at this point in the history
  • Loading branch information
calebdw committed Apr 14, 2024
1 parent 8bd32ca commit 320bcf0
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions docs/features.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Features

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

## Laravel 9 Attributes

Expand Down Expand Up @@ -36,3 +35,25 @@ protected function isTrue(): Attribute
}
```

## 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

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

0 comments on commit 320bcf0

Please sign in to comment.