How to set alias for php artisan in Windows

By: Roel
July 15, 2024

I’m going to share my experience setting aliases for common Laravel commands in Windows. As developers, our main goal is to automate things so we could save time for ourselves and for other people. Running php artisan commands over and over again can be a bit annoying so I’m going to share how I finally able to type “pa” instead of typing the whole “php artisan” command.

To be honest, it was not an easy thing to do. I guess it’s a lot easier for MAC users. First, I tried with the command prompt and then PowerShell. I was able to make it work with command prompt and PowerShell but only for temporarily. If you have come across this blog, you probably have done doskey a=php artisan and made it work but only if you don’t close the terminal. I struggled to make the alias work permanently. Having able to make it work with Git Bash is a lot easier so that what I’m going to share on this blog.

In most cases, when you are working with VS Code you are going to be interacting with Windows PowerShell. You can actually change that and use command prompt terminal and Git Bash. To switch just type CTRL + SHIFT + p and then a search bar would show up. Select “Terminal: Select Default Profile” then VS code will show you a bunch of terminal options. Then select Git Bash.

If you have work with Git, chances are you already have Git Bash in your system. If not, download Git Bash here  . Before, I proceed it is important to note that I am using Laravel Herd. 

1)     Locate PHP path installation in Laravel Herd

        The first thing to do when setting aliases on a Laravel Herd set-up is to set-up the path of the PHP installation on Laravel Herd. By default, the php is installed in C:/Users/YourName/.config/Herd/Bin.

            Note if you use XAMPP, this path should be different. You have to understand where PHP is installed with XAMPP.

2)     Specify this PHP path installation in Bash RC Source File (.bashrc)

         Bash RC Source File is typically found in Windows in C:/Program Files/Git/etc/.bashrc. Now open that file and include PHP path installation in the following format:

export PATH="/c/Users/YourName/.config/herd/bin/php83:$PATH"

       Take a look the php string after bin. You should include that. If you enable say PHP 8.2 in Laravel herd you have to include php 8.2. I enabled 8.3 so that’s why its 83

3)  After this you can already set aliases on the same file (.bashrc). You place it below export PATH line from step 2.

alias a="php artisan"
alias am="php artisan migrate"

       Save the aliases you set. Before this could take effect, close the Git Bash terminal in VS Code and then open again.

       These are only the 3 steps you need to set aliases for php artisan commands in Laravel in Windows with a Laravel Set-up. 

       If you have questions, comment down below.

Tags:

About the Author

Roel
Roel

Hi, my name is Roel. I am a TALL stack developer. I created this site to document all web applications I created using the TALL stack. I exclusively built products using Laravel because I like the experience writing applications using the simplest framework/stack available.



Please login to comment.

© 2024 Talldevelopers.com, All rights reserved.