Laravel and PostgreSQL Web App Part 1
If you’re a developer building web applications with the Laravel PHP web framework, you may need your app to interact with a PostgreSQL database. This multi-part series will take you through the process of building a web app that uses both Laravel and PostgreSQL. In this first installment of the series, we’ll show you how to set up the development environment needed for creating a Laravel and PostgreSQL web app on a local machine.
Before we begin working on our Laravel and PostgreSQL web app, we need to review the key prerequisites for the task:
- First, you’ll need to download and install Composer to help you install Laravel and to create your Laravel project(s).
- You’ll also need to to download and install XAMMP and run it as a service.
- Finally, you’ll need to have the text editor of your choice available. Throughout this tutorial, we’ll be using Visual Studio Code as our text editor.
NOTE: In this tutorial, we’re using the Windows operating system.
What is Laravel?
In this section, we’ll begin our tutorial with a brief overview of Laravel. You may wonder what Laravel is. Here are a few key facts about Laravel:
- Laravel is an open-source PHP web framework.
- Laravel is used to build database-driven websites and applications.
- Laravel uses the MVC (Model-View-Controller) design pattern.
What is MVC?
Let’s take a moment to take a closer look at the MVC design pattern and learn how it works. MVC is a common method for organizing code. Its name stands for “Model, View and Controller”– three integral components of an application’s design. We can define each of these elements in the following manner:
- Model – This is where the application interacts with databases or data-related logic such as CRUD (Create, Read, Update and Delete) operations.
- View – This part of the application encompasses things that an end user will see: HTML pages, the user interface and templates. The view is where the user interacts with the interface by performing actions such as clicking buttons, typing words or viewing content.
- Controller – This part of the application receives the POST or GET request made by the user within the browser and handles the request.
Now that we have a basic understanding of what Laravel is, we’ll proceed to install the framework on our machine. To do this, we’ll perform the following steps:
- First, we’ll navigate to the htdocs directory (C:xampphtdocs>). From there, we’ll execute the following command:
To verify that our project was successfully created, we can navigate to the directory in our browser using the following URL:
You should see something like this:
If we append the word ‘public’ at the end of our URL (
http://localhost/progjectmanager/public), we’ll see the following in the browser:
At this point, we’ve successfully created a new Laravel project; however, we’re not quite done. We still need to fine-tune our project a bit more.
Setting Up Laravel Virtual Host
In this section, we’ll be creating a virtual host. This host will enable us to use our custom URL that will point to this location:
To accomplish this task, we’ll perform the following steps in sequence:
- First, we’ll modify the
hhtpd-vhosts.conffile within our XAMPP application.
- Then, we’ll navigate to the following directory
C:\xampp\apache\conf\extraand modify the
hhtpd-vhosts.conffile by adding the following code:
The block of code shown above adds an entry for our project directory and core local host. This allows us to access our application using the
In order for the settings to run, we need to modify our system host file by navigating to the following URL:
We’ll modify the
hostsfile by adding the following entries:
NOTE: You can modify this file with any text editor of your choice, but be sure you have administrator rights.
- After saving the file, we’ll need to restart Apache with our XAMPP panel. We do this by clicking the stop and start button.
Now we can test our application and see if it will use the
projectmanager.test URL that we configured earlier.
Our output should look like this:
We can see that our Laravel application is now working properly with the new URL that we configured earlier.
If you’re creating a web app with the Laravel framework, you can design it to interact with a PostgreSQL database. Our multi-part tutorial series will show you how to build a Laravel PostgreSQL web app with step-by-step instructions. In this article, we first explained how to set up the development environment and install Laravel in your system; after that, we modified a couple of files to ensure that PHP could serve a custom URL. The next installment in this series will pick up where we left off and discuss the next steps involved in building the web app.
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.Get Started