Get "PHP 8 in a Nuthshell" (Now comes with PHP 8.3)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

Artisan command to connect to the database in CLI in Laravel 8.x

Laravel keeps getting better on each of its major releases. But sometimes, it’s the minor and patch releases that take the cake by introducing some lesser-noticed features.

For instance, recently, with the release of Laravel v8.16.0, it’s now possible to connect to the project’s databases right from the CLI using an Artisan command.

The artisan db command

A PR in v8.16.0 has introduced an artisan command php artisan db which is when run can connect to the default database of the project right in the CLI. As the PR author describes, it’s similar to what Rails has in the rails db command.

This command figures out which database you are using and starts a database CLI session based on your database configuration parameters defined in config/database.php.

So, for instance, if my project is using a MySQL database as its default database breeze and if I run the artisan db command, it will connect to the database like so.

php artisan db

Pretty nifty, right!

Specify connection explicitly

Apart from using the default php artisan db which connects to the default database, you can also explicitly specify the connection to connect to a non-default database connection like so.

$ php artisan db sqlite
Learn the fundamentals of PHP 8 (including 8.1, 8.2, and 8.3), the latest version of PHP, and how to use it today with my new book PHP 8 in a Nutshell. It's a no-fluff and easy-to-digest guide to the latest features and nitty-gritty details of PHP 8. So, if you're looking for a quick and easy way to PHP 8, this is the book for you.

Like this article? Consider leaving a


👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.