Unfortunately, PHPStorm does not have a built-in feature that automatically adds the use statement for a class when you're working inside a Blade file's @php directive. The IDE typically manages imports and fully qualified names (FQN) in PHP files, but Blade files are a bit different due to their nature of mixing HTML and PHP.
However, you can manually add the use statement at the beginning of your Blade file within the @php directive. PHPStorm will then recognize the imported class, and you can use the short name within the rest of your @php block.
Here's how you can manually add the use statement:
@php
use Livewire\Volt\Component;
new class extends Component { ... }
@endphp
After adding the use statement, PHPStorm should provide autocompletion for the Component class without the FQN within the @php block.
If you find yourself frequently needing to add use statements in Blade files, you might consider creating a Live Template in PHPStorm to speed up the process. Here's how you can create a Live Template:
- Go to
PreferencesorSettingsin PHPStorm. - Navigate to
Editor>Live Templates. - Click the
+icon to add a new template. - Choose the applicable context for Blade files.
- Define a template abbreviation and description.
- Enter the template text, for example:
@php use $CLASS$; $END$ @endphp - Define the variables (
$CLASS$and$END$) if necessary. - Save the template.
Now, when you type the abbreviation you defined in a Blade file and press the tab key, PHPStorm will insert your template, allowing you to quickly add a use statement.
Remember that Blade files are compiled into cached PHP files, so any PHP code you write inside @php directives should be valid PHP code. Adding use statements at the top of these blocks is perfectly acceptable and will be understood by the PHP engine when the Blade template is rendered.