For $item
there’s a polymorphic loggable
relationship. In the database this is stored in items
as loggable_type
and loggable_id
(for PHP 8+ in and Laravel).
for($items as $item) {
// ...
if ($item->loggable_type === Comment::class) {
$item->loggable->resetDates();
}
// ...
}
I’m trying to type hint that loggable
within the condition is a Comment
. I think I might be able to user @var
but doing something like /* @var $item->loggable Comment */
doesn’t work and I can’t do /* @var $item Comment */
since that sets it to $item
and not its property.
I’m thinking of changing the condition to if ($item->loggable instance of Comment)
, however I’d rather not since that requires loading and creating the Comment even if it’s not used.
Is there a way to type hint $item->loggable
as Comment
?
2
Answers
Assign it to variable
As @Justinas stated, either assign it to a variable:
Or set the possible values you could have (if they are fixed and known) on the model itself like this:
That will tell the IDE that the property
loggable
is possibly aComment
object,WhateverModel
object, orModel
object (so you still have normal Model’s methods auto-completion).If you can narrow down which classes you could have there, then add them, else I would go with the first option (assign a value and use a PHPDoc block for a variable)