So I went to read up on some Laravel documentation regarding Homestead because you know, Docker on Mac, arghhh, well that is a story for another day.
Anyway, Laravel 9 is out and one of the features that stood out for me was the support for generic types. If you do not know what these are, there is a great article by Ondřej Mirtes regarding them here.
What does this have to do with Laravel? Well, I am coming to that in a minute. One of my favourite features in Laravel is Eloquent, more specifically, Eloquent relationships. But there has always been something missing, whenever you get the collection instance, the IDE does not know what types are inside the collection.
Consider the code below, I have been using generic type hinting for a while in my code, more in a documentation capacity.
* @property Collection<Notification> $notifications
If I call the notifications property, I correctly get that it is an instance of
Collection but what I don't get is that the contents are instances of
$notifications->first() will simply be an object with no type. I will need to type hint the resulting object if I wanted to get autocomplete.
With this change in Laravel 9, the extra comment type hint is now unnecessary. I can simply type-hint the collection with a generic like above and get the correct instance. This will help clean up a lot of unnecessary comments in the code.
Anyway, if that does not get you excited, you can check out some of the other features here.
Top comments (1)