Published on

Auto Formatting Laravel in VSCode

Laravel has a great package called Laravel Pint that can be used to format your code following a standard set of rules. It's a great way to keep your code consistent and clean. You can setup CI/CD to run Pint on your codebase, but it's also nice to have it run locally as you're working on your code. There are a few ways to do this, and a dedicated VSCode extension but I prefer setting it up myself with Run On Save.

To get started make sure to download the VSCode extension Run On Save.

Once you have done that, in the root of your project create the following file:

.vscode/settings.json

Then add the following to the file:

{
  "emeraldwalk.runonsave": {
    "commands": [
      {
        "match": "\\.php$",
        "isAsync": true,
        "cmd": "./vendor/bin/pint ${file}"
      },
    ]
  },
}

Now anytime you save a PHP file in your project, Pint will run and format your code for that specific file. You can also add additional commands to run on save for other file types and perform various action. The Run On Save extension is very powerful and can be used for a lot of different things. Overall, it's a great way to keep your code clean and consistent as you're working on it.