IntelliJ IDEA 2023.1 Help

Laravel

Laravel is a free, open source PHP web application framework. It is built on top of several Symfony components, and makes common tasks such as authentication, routing, sessions and caching much easier to implement.

Laravel Idea plugin

As any other PHP project, Laravel projects are extensively backed by IntelliJ IDEA features as is. For extra IDE support of Laravel-specific features, such as code generation, eloquent completion, or autocompletion for routes, request fields, and validation rules, use the Laravel Idea (paid) plugin. This plugin is not bundled with IntelliJ IDEA and needs to be installed and enabled on a per-project basis.

For the full list of features provided by Laravel Idea and instructions on how to use them, see the official documentation.

Blade templates support

Before you start, make sure the Blade plugin is installed and enabled. The Blade plugin is not bundled with IntelliJ IDEA, but it can be installed on the Settings | Plugins page, tab Marketplace, as described in Installing plugins from JetBrains repository.

IntelliJ IDEA provides full support of the Laravel Blade template engine. It highlights various Blade syntax constructs, as well as any HTML, JavaScript and CSS code inside the templates.

Besides syntax highlighting, IntelliJ IDEA provides several other Blade-specific features.

Code completion for braces and directives

IntelliJ IDEA's editor provides code completion both for standard and custom Blade directives, which can be defined In the Settings dialog (Control+Alt+S) under Languages & Frameworks | PHP | Blade.

Laravel Blade directive completion

When @for or @foreach directives are used, variable introduction with code completion is available inside the construct's body.

Laravel Blade variable introduction completion

Sections support

While working on a Blade template, you can open a section using the @section directive. IntelliJ IDEA provides code completion Control+Space for all known sections' names in the project.

Laravel Blade section completion

IntelliJ IDEA provides the code inspection that detects the sections that are not closed using the @stop directive.

Laravel Blade closed section inspection

To navigate to the declaration of a section, position the caret at its usage and press Control+B. Alternatively, Control+LeftClick the usage.

The Laravel plugin also adds a marker to the editor gutter, which lets you navigate to the parent section.

Navigate to the parent section via the gutter icon

Code completion and navigation for extends and includes

Blade templates are often composed of various includes of small reusable blocks, which are in turn other templates. You can also extend templates and provide content for additional sections. IntelliJ IDEA and the Laravel plugin provide completion for template names in both the @extends and the @include directives. Completion suggestions include template directory names as well as full template names.

Laravel Blade @extends directive completion

To navigate to the declaration of a template, position the caret at its usage and press Control+B. Alternatively, Control+LeftClick the usage.

Language injection in Blade templates

When working with Blade templates, you can inject code fragments inside the template blocks. IntelliJ IDEA will provide you with comprehensive language assistance for editing that code fragment.

Inject JavaScript or CSS into a Blade template section automatically

IntelliJ IDEA can automatically inject code into Blade template sections based on the defined injection rules. Out of the box, the rules for automatically injecting JavaScript and CSS code are available.

  • In a Blade template, add a section named javascript (to inject JavaScript) or css (to inject CSS) as follows:

    @section('javascript') // injected JavaScript code @stop @section('css') // injected CSS code @stop

    IntelliJ IDEA will automatically inject JavaScript or CSS into the template sections.

    Automatically inject JavaScript and CSS in Blade a template

Debug Blade templates

You can debug Blade templates using the same techniques as for regular PHP files.

Enable Blade debugging

  1. In the Settings dialog (Control+Alt+S), go to Languages & Frameworks | PHP | Debug | Templates and expand the Blade Debug area.

  2. In the Cache path field, provide the absolute path to the Blade compiled templates cache folder. Type the path manually or click the Browse button and select the relevant folder in the dialog that opens. By default, compiled Blade templates are stored in the storage/framework/views/ folder inside your project.

Start a debugging session

Start a debugging session as described in the Ultimate debugging guide. The easiest and recommended approach is to use Zero-configuration debugging:

  1. Choose and install the browser extension suitable for your browser.

  2. On the IntelliJ IDEA toolbar, toggle the Start Listening for PHP Debug Connections button to start listening for incoming PHP debug connections, or choose Run | Start Listening for PHP Debug Connections from the main menu.

  3. Set a breakpoint in your code.

  4. Start the debugging session in the browser using the installed browser extension.

  5. During a debugging session, examine the program state: see variable values, evaluate expressions, step through the program, and so on.

Configure Blade templates

Add, modify, or remove Blade directives

Blade directives are managed on the Directives tab of the Blade Page. The tab lists all the currently available Blade directives, for those that have parameters, the prefixes and suffixes are also shown. When you start, the list contains only predefined directives. You can edit these directives as well as create custom ones.

  1. In the Settings dialog (Control+Alt+S), go to Languages & Frameworks | PHP | Blade.

  2. On the Blade page that opens, switch to the Directives tab, which shows a list of all currently available directives.

    • To define a new directive, click the Add button and specify the directive's name in the Name field.

      If the new directives requires a prefix and a suffix, select the Has parameter checkbox and type the prefix and suffix to use in the Prefix and Suffix fields respectively. IntelliJ IDEA will automatically enclose the prefix and suffix in opening and closing brackets and quotes and add a colon separator : so the parameters will look as follows: ("<prefix>:<suffix>").

    • To edit an existing directive, select it in the list and change the values in the fields below.

      To restore the original definition, click the Reset to defaults button.

    • To remove a directive from the list, select it and click the Remove button.

Configure Blade delimiters

IntelliJ IDEA recognizes Blade templates and provides error highlighting and code completion for them based on the delimiters you specify.

  1. In the Settings dialog (Control+Alt+S), go to Languages & Frameworks | PHP | Blade.

  2. On the Blade page that opens, switch to the Text Tags. The fields in the tab show the opening and closing characters for raw tags, content tags, and escaped tags.

  3. The fields are filled in with the default values in compliance with Blade Templates 5.8. If you are using an earlier version, you can specify the relevant custom delimiters and IntelliJ IDEA will provide coding assistance according to the new rules.

Use the Artisan command line tool from IntelliJ IDEA

IntelliJ IDEA integrates with the Artisan command-line interface, which is included with Laravel and provides several handy commands.

Configure Artisan automatically

Configure Artisan manually

  1. In the Settings dialog (Control+Alt+S), go to Tools | Command Line Tool Support.

  2. Click the Add button on the toolbar.

  3. In the Command Line Tools dialog, choose Laravel from the list, and specify its visibility level (Project or Global).

    the Command Line Tools dialog
  4. When you click OK, the tool settings dialog opens.

    Specify the tool alias, provide the path to artisan, and choose one of the configured PHP interpreters from the PHP Interpreter list. See Configuring Local PHP Interpreters and Configuring Remote PHP Interpreters for details.

    the artisan command-line tool settings
  5. Click OK to apply changes and return to the Command Line Tool Support page. Optionally, click the Edit button to edit the tool properties, or the Edit Source button to customize the commands set. See Customize a tool for details.

Run Artisan commands

  • From the main menu, choose Tools | Run Command or press Control twice.

    In the Run Anything window that opens, type the call of the command in the <artisan> <command> format.

    The command execution result is displayed in the Run tool window.

    Running a artisan command

Terminate a command

Debug Artisan commands

Laravel commands are defined in controller classes that extend Command. To debug a command, it is crucial that you initiate a debugging session for the command itself, and not the controller class file it is defined in. Otherwise, the Laravel bootstrapping process will be skipped, and the execution will fail.

  1. In the controller class corresponding to the selected command, click the editor gutter at a code line where you want to set a breakpoint.

    Set a breakpoint in a Laravel command
  2. Create a run/debug configuration that will run the artisan tool with the selected command. In the main menu, select Run | Edit Configurations, then click the Add button and choose PHP Script from the list.

    Run/Debug configuration: PHP script
  3. In the PHP Script dialog, provide the run/debug configuration parameters.

    • In the File field, provide the path to the artisan executable file.

    • In the Arguments field, type the actual command and its arguments, such as view:cache.

    Run/Debug Configurations dialog for Laravel CLI command
  4. On the IntelliJ IDEA toolbar, select the created run/debug configuration and click the Debug button. The command execution will stop at the specified breakpoint.

    Debug a Laravel command
Last modified: 26 June 2023