Add Aiven database magic to your Laravel project
Our mission at Aiven is to make developers’ lives better. Today I’d like to share our new tool that makes Laravel developers’ lives better by adding some helpful functionality to their projects.
Laravel is the most popular PHP framework in use today, and is usually backed by either MySQL or PostgreSQL databases (Aiven offers both). Many developer platforms use one database for development and another for specific sets of test data. You might also need to connect elsewhere to debug a specific problem. However, switching between databases can be cumbersome.
Luckily we have a solution for you! The
aiven-laravel package makes it painless to reconfigure your database connection. It adds commands to the
artisan command line utility that you can use to configure your application to point to any of your Aiven database services. It also adds some helpers to power off any databases that are not in use, so you can save that money for a more productive purpose.
Set up the Aiven-Laravel package
aiven-laravel to your existing (or newly created) project with Composer; the project page has the full installation instructions. You will need to generate an Aiven access token to connect your Aiven account, and then you are ready to Aiven your Laravel ... (is Aiven a verb? It is now).
Configure Laravel to use an Aiven service
aiven-laravel makes setting the configuration easier whether you're running MySQL or PostgreSQL on Aiven (or indeed Redis or OpenSearch, which are also common and sound combinations for Laravel). The default configuration for Laravel is to use a
.env file and to list the database host, port, and other details separately. However, it also supports use of the
DATABASE_URL environment variable to hold an entire connection string. This can be a more convenient way to work with database connections. Set the single string in your
.env file for development, and configure just one environment variable on the cloud platform you deploy to.
To check the databases and their types that are currently in your Aiven account, use this command:
php artisan aiven:list
Note: If you installed the
aiven/aiven-laravel package without reading the instructions and didn't configure it yet, it will prompt you with the environment variables you need to set. Because, yes, this package was built by an engineer who doesn't read setup instructions either.
Let’s say you have a MySQL service called
dev-db. You can get the configuration that you need to connect to it from Laravel with:
php artisan aiven:getconfig --service dev-db
Take the output of the command and put it wherever you need your config to go! If you’re developing locally, this is
.env by default, but if you use multiple environment config files or are managing your environment differently, you can use these values there instead.
Avoid cloud bill surprises
One thing that stops people from modernizing their setup and moving to the cloud, especially for development, is how easy it is to accidentally misconfigure something and get a large bill. It happens too often, and it’s definitely something that all developers should be cautious of. Aiven’s pricing includes data transfer so the price shown is what you’ll pay if you leave the database running full time — this by itself reduces the risks hugely.
Another way to keep costs under control is to turn things off when you are not using them, and this applies particularly to development platforms. Aiven gives the ability to power off your database when you’re not using it, and power it back on unchanged — and we’ve added support to do that from
artisan with the Aiven-Laravel package.
When you sit down to do some development work and need your database:
php artisan aiven:powerup --service dev-db
[your amazing things happen here]
When you have finished for the day, stop the meter from running by powering the service down again until next time:
php artisan aiven:powerdown --service dev-db
For databases that don’t need to be always on, the test platforms for a client that you don’t need to use every day … this approach makes the cloud a very attractive place to keep your databases for low-friction access when you need them.
Aiven-Laravel and you
The Aiven-Laravel package is a new release, we know that plenty of PHP developers use our platform already, and we would love to hear from you if you find the tool useful (or not!). What would you add? Is there anyone you think should try out the tool? Issues, stars and pull requests are more than welcome on the GitHub repository, we’re excited!
If you’re not using Aiven services yet, go ahead and sign up now for your free trial at https://console.aiven.io/signup!
Originally published at https://aiven.io.