[Feature Request]: Add support model relationship/attribute Hints
Feature Description
Request
Add support in ide.json for model relationship/attribute hints similar to how laravel-data handles relational lazy
properties.
Example:
Lazy::whenLoaded('avatar', $user, fn() => $user->avatar);
In this example, avatar is a model relationship on $user. It would be useful if ide.json could provide intelligent type hints or completions for such relationships.
There is a model-property rule in Larastan that seems to perform similar checks.
Perhaps it could be leveraged, or alternatively, an ide.json configuration could be provided — But this one looks like it’ll probably be quite complicated.
Example Reference
Model\User.php
/**
* @param model-property<User> $attribute
*/
public function getAttributeOr(string $attribute, ?Closure $default = null): mixed
{
if ($this->hasAttribute($attribute)) {
return $this->getAttribute($attribute);
}
return value($default);
}
Helper.php
/**
* @template T of Model
* @param model-property<T> $attribute
*/
public function getModelAttributeOr(Model $model, string $attribute, ?Closure $default = null): mixed
{
if ($model->hasAttribute($attribute)) {
return $model->getAttribute($attribute);
}
return value($default);
}
- Demo
Helper::getModelAttributeOr($user, 'username')
// ⬆️Hints Here
$user->getAttributeOr('username')
// ⬆️Hints Here
Currently, for some existing completion hints, ide.json can configure the place where they take effect, which is cool.
We have some plans for ide.json, which will cover getModelAttributeOr and getAttributeOr cases. I hope we will implement them soon.