Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How can I create custom snippets for Laravel 11 using Visual Studio Code


How can I create custom snippets for Laravel 11 using Visual Studio Code


To create custom snippets for Laravel 11 in Visual Studio Code, you can follow these steps:

1. Open the Command Palette in VS Code by pressing `Ctrl+Shift+P` (Windows/Linux) or `Cmd+Shift+P` (Mac).

2. Type "Configure User Snippets" and select it from the list.

3. Choose the language you want to create snippets for, in this case, select "PHP".

4. This will open the `php.json` file where you can define your custom snippets.

Here's an example of how you can create a snippet for a Laravel 11 controller:

json
"Laravel 11 Controller": {
    "prefix": "laravel11-controller",
    "body": [
        "<?php",
        "",
        "namespace App\\Http\\Controllers;",
        "",
        "use Illuminate\\Http\\Request;",
        "",
        "class ${1:ControllerName} extends Controller",
        "{",
        "    /**",
        "     * Display a listing of the resource.",
        "     *",
        "     * @return \\Illuminate\\Http\\Response",
        "     */",
        "    public function index()",
        "    {",
        "        //",
        "    }",
        "",
        "    /**",
        "     * Show the form for creating a new resource.",
        "     *",
        "     * @return \\Illuminate\\Http\\Response",
        "     */",
        "    public function create()",
        "    {",
        "        //",
        "    }",
        "",
        "    /**",
        "     * Store a newly created resource in storage.",
        "     *",
        "     * @param  \\Illuminate\\Http\\Request  \$request",
        "     * @return \\Illuminate\\Http\\Response",
        "     */",
        "    public function store(Request \$request)",
        "    {",
        "        //",
        "    }",
        "",
        "    /**",
        "     * Display the specified resource.",
        "     *",
        "     * @param  int  \$id",
        "     * @return \\Illuminate\\Http\\Response",
        "     */",
        "    public function show(\$id)",
        "    {",
        "        //",
        "    }",
        "",
        "    /**",
        "     * Show the form for editing the specified resource.",
        "     *",
        "     * @param  int  \$id",
        "     * @return \\Illuminate\\Http\\Response",
        "     */",
        "    public function edit(\$id)",
        "    {",
        "        //",
        "    }",
        "",
        "    /**",
        "     * Update the specified resource in storage.",
        "     *",
        "     * @param  \\Illuminate\\Http\\Request  \$request",
        "     * @param  int  \$id",
        "     * @return \\Illuminate\\Http\\Response",
        "     */",
        "    public function update(Request \$request, \$id)",
        "    {",
        "        //",
        "    }",
        "",
        "    /**",
        "     * Remove the specified resource from storage.",
        "     *",
        "     * @param  int  \$id",
        "     * @return \\Illuminate\\Http\\Response",
        "     */",
        "    public function destroy(\$id)",
        "    {",
        "        //",
        "    }",
        "}"
    ],
    "description": "Create a new Laravel 11 controller class"
}

In this example:
- `"prefix"` specifies the trigger for the snippet (type this and press Tab to expand the snippet)
- `"body"` contains the actual code snippet
- `"description"` provides a brief description of the snippet

You can customize the snippet to include your preferred code structure and placeholders (denoted by `${1:placeholder}`) for easy navigation.

After saving the `php.json` file, you can use the snippet by typing the prefix (`laravel11-controller`) in a PHP file and pressing Tab. The snippet will expand, and you can navigate through the placeholders using the Tab key.

You can create similar snippets for other Laravel 11 components, such as models, views, and more, following the same structure.

Citations:
[1] https://marketplace.visualstudio.com/items?itemName=nabeyasu112358.Laravel-11-Snippets
[2] https://stackoverflow.com/questions/46339389/display-code-snippet-in-laravel-blade
[3] https://laravel.com/docs/11.x/releases
[4] https://laravel.com/docs/11.x/structure
[5] https://laraveldaily.com/post/laravel-11-main-new-features-changes