PHP-Doc in Blade-Views
In my Telegram Newsletter post I've used PHP-doc type-hints without explaining them as I'm doing it every day. In the comments @alexfwulf mentioned that this was learning for him. So I'm dedicating a whole post on that topic.
With Laravel 7 we got Blade Components which are beautiful but also introduced even more ways to inject variables in a Blade View and for components we get some default variables.
Even before it was hard to know which variables are available in a Blade View and even harder what type they're.
The Laravel IDEA plug-in gets better and better in resolving them. But isn't perfect so some manual work is still required. My solution is to put PHP-doc variable type-hints to the start of my Blade files. This way I immediately know which variables I can use and what type they're.
I'm using a single line and PHP-tag per type-hint. This way it's easier to copy or remove them.
<?php /** @var \Illuminate\Support\Collection|\App\Models\User[] $users */ ?>
<?php /** @var string $name */ ?>
After I've added all the tags I can use the variables and can use PhpStorm awesome suggestions/autocompletion.
Blade components
I start every Blade component with one to two tags as these are default component variables.
Primary the $attributes
variable has a lot of undocumented methods you can use to do cool things like cherry-picking special attributes.
<?php /** @var \Illuminate\View\ComponentAttributeBag $attributes */ ?>
<?php /** @var \Illuminate\Support\HtmlString $slot */ ?>