Laravel is a great PHP based software development framework to develop Web based software applications. PHPStorm is a Lightning-Smart PHP IDE that can really fuel your PHP software development process and combined with Laravel it really helps developers to be suoer productive.

For the most part there is not really that much set up required make PHPStorm to work with a Laravel project and if you have installed and configured Laravel on your Ubuntu Desktop you are pretty much good to go. All you need to do open your project directory in in PHP storm and you can start coding!

  1. In PHPStorm 6 to connect to a vagrant hosted VM's MariaDB/MySQL, open the Database Window (View Tool Windows Database) and add a MySQL connection using the wizard. The trick is what to put in where. If it helps, permit me to explain in full how I connect, using an identity file (also shown in the vagrant ssh-config utility).
  2. I ran a sniffer (tcpdump) on the VM and there were NO connection attempts when trying to use phpStorm to connect to mysql. Just to verify, I ran mysql -h 192.168.22.20 -u bogus -p bogus I was able to connect to mysql and I saw the captured packets in tcpdump. I then tried to connect to the port-forwarded address from the VM.
  3. In PhpStorm, you export object structures and data separately. It means that you can export a structure of a table and then export data from the table. The full data dump is available for PostgreSQL and MySQL with the help of mysqldump and pgdump.
  4. It is possible to connect to your mySQL database in your Homestead environment using PHPStorm all that is required is some additional configuration. Open the Database window. To open your Database window in PHPStorm simply click on VIew – Tool Windows – Database and the Database Sidebar will open. Configure SSH Tunnel.

Open your project in PhpStorm and open the DataSource window: Click on View - Tool Windows - Database Click on the Database sidebar, click on new - Datasource.

The Laravel Homestead is an official, pre-packaged Vagrant box that provides you a wonderful development environment without requiring you to install PHP, a web server, and any other server software on your local machine. No more worrying about messing up your operating system! Vagrant boxes are completely disposable. If something goes wrong, you can destroy and re-create the box in minutes!

It works great, and for the most part developers never really need to concern themselves with the minutae of working with the various components involved.

Working with Databases on Homestead

Phpstorm Connect To Mysql

Laravel comes prepackaged powerful REPL CLI , called Tinker, powered by the PsySH console by Justin Hileman under the hood. The tinker console allows you to interact with your Laravel application from the command line in an interactive shell.

Using Tinker we can interact with the database through command-line and can even test and use Tinker to add dummy data to your database table and you don’t have access to the database. In such cases, you can insert data through Tinker. We can even update, delete table records in the database.

This is great tool, but occassionally seasoned developers would like to connect directly to their databases to write complex SQL queries or even just to check schema changes or maybe even to optimise the databases etc.

PHPStorm comes with great features to enable to connect to a number of different databases directly within the IDE. This is a great feature which helps developers to troubleshoot and debug database issues directly within there development environment.

It is possible to connect to your mySQL database in your Homestead environment using PHPStorm all that is required is some additional configuration.

Open the Database window

To open your Database window in PHPStorm simply click on VIew –> Tool Windows –> Database and the Database Sidebar will open

Configure SSH Tunnel

When creating a Homestead Environment all components and databases etc utilised by your Laravel application are contained within your Vagrant Machine. The only way you can access these utilities are by using SSH. Fortunately, PHPStorm enables you to connect to database making use of SSH Tunnels.

In order to do so we will need to get the SSH Configuration details, which fortunately Laravel makes super easy for us with a simple Terminal Command we can use. So by accessing the PHPStorm integrated terminal window and ensuring we are in the project root directory we can use the following command

which will provide a result similar too

We are now going to use these details in order to connect to our database in the Homestead environment

Using the Database window click on the Plus icon –> DataSource –> mySQL

When the Data Sources and Drivers window opens select SSH/SSL tab and ensure Use SSH Tunnel is selected

We can now enter the data we retrieved from our ssh-configuration as follows:

Phpstorm Mysql Docker

Once all the data has been completed. Switch back to the General tab and then add in your database credentials. For the sake of this article we’re going to use the default homestead credentials.

We can then click Test Connection and get a confirmation that our connection was successful.

Summary

In this post we covered how to connect PHPstorm to your Laravel Homestead Database.

Phpstorm Mysqldump

Mysql

Phpstorm Mysql

  • How to create project templates in .net core - April 24, 2021
  • Book Review: Building Event-Driven Microservices - April 23, 2021
  • Implementing logging with Serilog - April 17, 2021