Laravel#
PHP extension version >=
1.55
.
Support for Laravel framework is built-in. The editor provides additional code completion, navigation, syntax highlighting, .blade.php
editor, code formatting style for Laravel, and debugging using built-in server.
Blade Templating#
The editor for blade.php
files is extended with PHP syntax support, code folding, Blade directives completion, Laravel Components and Livewire Components completion and navigation, and Blade file formatting.
Blade File Formatting#
The formatter is built-in and provides code formatting for HTML, CSS, JS, and PHP, and the structure of Blade file. Use Format Document
editor command.
The configuration respects configurations of PHP, HTML, JS, CSS settings in VSCode respectively, and no additional setting is needed.
Smart Braces Completion#
Since
1.56
Brace pairs are automatically completed and space added for special tags {{ }}
, {{{ }}}
, {!! !!}
and {{-- --}}
.
PHP Editor inside Blade Directives#
Nested PHP code inside .blade.php
files is treated as regular PHP code - all the PHP editor features are enabled including code completion, navigation, tooltips, diagnostics, inlay hints, code lenses, or formatting.
Blade Directives Completion#
Blade directives are completed upon typing @
character. Completions representing a snippet are annotated with a snippet icon.
The list contains common Blade directives, directives defined within user code using Blade::directive()
and Blade::if()
methods, and directives defined in ide.json
customization file.
Components Completion and Navigation#
Blade views, components, and Livewire components are scanned in default Laravel directories. View classes and .blade.php
files are suggested using the Laravel view notation:
- As HTML tags after
<x-
, or inside@include
,@each
and other directives. - Livewire components after
<livewire:
, and inside@livewire()
directive. - In
view()
function and similar.
Additionally, component attributes are suggested in related functions.
Component attributes defined as view class properties, view @props
, or Livewire mount()
parameters are suggested/navigated to.
Component Attributes and Livewire Actions#
Since
1.56
Component classes exposing properties and Livewire actions are used to provide additional completions of variables and possible values for wire:
attributes.
Sections Completion#
Since
1.56
Section names are collected across the Blade files extended by or extending the current view. Sections are then suggested in appropriate directives.
Switch between Blade View and Class#
Since
1.56
To switch quickly from a view file to its corresponding class, and vice versa, create a keyboard shortcut and associate it with "phptools.blade.switchViewClass"
command.
- Open Visual Studio Code, navigate to Preferences and Keyboard Shortcuts (or use command palette
> Open Keyboard Shortcuts
): - Search for
phptools.blade.switchViewClass
, and associate it with your keyboard shortcut:
Laravel Code Formatting Style#
Please see Code Styles and make sure to switch to Laravel
code style to enable all Laravel formatting rules.
Facades, Services, and Class Aliases#
The editor recognizes class aliases such as Route
defined across Laravel framework and in app
configuration. Class aliases are listed in PHP code completion togeher with other symbols.
Laravel is heavily based on service containers and dependency injection. Registered Laravel services from across the Laravel project are recognized and used for additional code completion after app()
or in Laravel facades.
The editor traverses through Laravel Facades, their accessor service, and recognizes completion methods. In case the facade accessor service can't be recognized using static code analysis, annotate the Facace class with @mixin
PHPDoc keyword, for example:
Eloquent Models#
Since
1.56
Eloquent query builder has full code completion including extensive generic types inferrence, and completion of model columns defined by its model factory. Note, this works best in newer Laravel framework versions (9 and newer, 11+ preferably) since they are better annotated.
Code completion provides local-scoped functions out of the box, and where
functions for model columns, without additional annotation or configuration needed.
Eloquent model table columns are resolved from (what's defined first):
- model class PHPDoc
@property
(s). - models Factory class
definition()
function. - using models table name (if can be resolved), and by looking up migrations in
database/migrations/
andSchema
definitions.
columns are not obtaind directly from database.
Completion and Navigation in Laravel-Specific Functions#
Special Laravel functions such as __()
, lang()
, @lang()
blade directive, config()
, env()
, storage_path()
, asset()
, view()
, @include()
-like blade directives, mix()
, route()
, View::make()
, and others have additional code completions and navigations.
- Completion inside env() function uses
.env
file in workspace root directory. - Completion inside __(), or lang() looks for
.json
translation files in/lang
or/resources/lang
. - Completion for config() is collected from arrays in
/config
directory.
Route Completion#
Named routes are identified within /routes/
directory. Route name and the route's parameters are suggested in route()
function (and related functions) together with detailed tool-tip.
Laravel Idea ide.json
Support#
Since
1.56
Additional code completions, blade directives, and blade components defined within special ide.json
file(s) are supported. The ide.json
can be placed anywhere in the VS Code workspace.
Debugging using Built-in Server#
Please see debugging overview for configuring PHP and Xdebug binaries first. Note, you don't need additional VSCode extensions.
Without Launch Profile#
Without .vscode/launch.json
launch file, simply press F5
(Run / Start Debugging) with any opened .php
file. VSCode will popup the following pre-configured profiles which initiate built-in Web Server with debugging enabled for the Laravel application.
Launch built-in Server and Debug
starts built-in development Web Server (using selected PHP), with Laravel routing, .env
file, and debugging enabled.
Launch Profile#
For more customization, create a launch profile, and select PHP. The profile will be pre-configured for use with Laravel.
Please watch DEBUG CONSOLE window for the development server log, or issues.
See Also#
- Selecting PHP Version: PHP Version
- Configuring code diagnostics: Problems
- Installing packages using Composer: Composer
- Running PHPUnit Tests: Test Explorer
- Code Formatting Style: Code Styles
- Profiling Laravel App Tutorial: Profiling Laravel App in VS Code