Skip to content

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#

blade file

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

smart blade braces completion

Brace pairs are automatically completed and space added for special tags {{ }}, {{{ }}}, {!! !!} and {{-- --}}.

PHP Editor inside Blade Directives#

php in blade IntelliSense

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 directive 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.

blade include

Components Completion and Navigation#

component competion

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.

view completion

Additionally, component attributes are suggested in related functions.

view completion

Component attributes defined as view class properties, view @props, or Livewire mount() parameters are suggested/navigated to.

component competion

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.

Livewire Actions and Component Porperties in Blade Files

Sections Completion#

Since 1.56

Blade Sections Completion

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.

  1. Open Visual Studio Code, navigate to Preferences and Keyboard Shortcuts (or use command palette > Open Keyboard Shortcuts): VSCode Keyboard Shortcuts Menu
  2. Search for phptools.blade.switchViewClass, and associate it with your keyboard shortcut: Blade Switch View Shortcut

Laravel Code Formatting Style#

Please see Code Styles and make sure to switch to Laravel code style to enable all Laravel formatting rules.

laravel code style setting

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 service completion

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.

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:

/ ** @mixin \Illuminate\Cache\Repository * /
class Cache extends Facade { ... }

Eloquent Models#

Since 1.56

eloquent model member completion

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.

eloquent local scopes

Code completion provides local-scoped functions out of the box, and where functions for model columns, without additional annotation or configuration needed.

eloquent where clausule

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/ and Schema definitions.

columns are not obtaind directly from database.

Completion and Navigation in Laravel-Specific Functions#

view completion

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 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.

route parameter completion

Laravel Idea ide.json Support#

Since 1.56

laravel idea ide.json

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.

laravel debug

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#

select 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#