Laravel and PostgreSQL Web App Part 2
Introduction
If you’re harnessing the power of the Laravel PHP framework to build a web app, you may want to know how to interact with a PostgreSQL database within your app. This tutorial series walks you through the entire process of creating a Laravel PostgreSQL web app. The first article in the series covered how to set up the development environment for the app and how to install Laravel. In this second installment, we’ll show you how to create the model and controller for our Laravel web app.
Prerequisites
Before proceeding with the instructions outlined in this tutorial, be sure to read and complete the steps described in the first article of this series. In addition, you’ll need to have PostgreSQL installed and configured on your machine in order to follow along with this installment of the tutorial.
Creating a PostgreSQL database
In this section, we’ll show you how to create the sample database that we’ll work with as we create our Laravel and PostgreSQL web app. Here are the steps we’ll perform to accomplish this task:
First, we’ll open up
pgAdmin
and provide our user credentials.Next, we’ll right-click Database and select Create; in the sub-menu, we’ll select Database…
- We’ll need to provide a meaningful name for the database. For this example, we named our database ‘projectmanager’.
- Finally, we click the Save button to create the database.
Configure PostgreSQL in Laravel
Now that we’ve set up our sample database, we’ll work on configuring PostgreSQL in our Laravel application. To do this, we’ll follow these steps in sequence:
First, we’ll open up the Laravel project directory, which contains the files we need to modify. You can use any text editor of your choice to edit these files. In this tutorial, we’ll be using Visual Studio Code as our text editor.
Once the Laravel project directory is opened, we’ll modify
database.php
by going in theconfig
folder as shown in the image below:
We can see that PostgreSQL defaulted to mysql
. Let’s change that to pgsql
so that PostgreSQL will work in Laravel.
- Next, we’ll need to configure our database credentials in Laravel by updating the
.env
file:
The details highlighted in the image shown above will be modified to reflect the following values:
1 2 3 4 5 6 | DB_CONNECTION=pgsql DB_HOST=127.0.0.1 DB_PORT=5432 DB_DATABASE=projectmanager DB_USERNAME=postgres DB_PASSWORD=1234 |
These details provide the name of the database connection driver pgsql
within the the localhost server 127.0.0.1
. The details also indicate that the default port is 5432
, the desired database is projectmanager
, the default user is postgres
and the password is 1234
.
Setting up the Laravel Controller
Our next step will be to configure our Laravel Controller.
To do this, open up your terminal and navigate to your project directory. If you’ve been following along, you should be in the following directory: C:\xampp\htdocs\projectmanager>
Then, execute the following command:
1 2 | C:\xampp\htdocs\projectmanager>php artisan make:controller PorjectsController Controller created successfully. |
The response lets us know that our controller was successfully created.
Setting up the Laravel Model
In the previous section, we showed you how to create a Laravel controller using ‘artisan’. Next, we’ll be using the same tool to create the model file for our application.
The process is quite similar to the one in the previous section. Open up your terminal and navigate to your project directory, then execute the following command:
1 2 3 | C:\xampp\htdocs\projectmanager>php artisan make:model Project -m Model created successfully. Created Migration: 2019_11_13_070146_create_projects_table |
In this command, we used a flag -m
to create a migration file as shown in the output Created Migration: 2019_11_13_070146_create_projects_table
.
Now that we’ve created our model, let’s tweak it a bit to meet the requirements of our application.
We’ll start by modifying the migration file that we created earlier. We’ll go to the database folder and then to the migrations folder, and we’ll open up the 2019_11_13_070146_create_projects_table.php
migration file.
The above image shows that only the id
and the timestamps
will be created; however, we’re going to need more than that. Let’s append the following details that are required by our application:
1 2 3 4 5 6 7 8 9 10 | public function up() { Schema::create('projects', function (Blueprint $table) { $table->bigIncrements('id'); $table->integer('user_id'); $table->string('name'); $table->string('description'); $table->timestamps(); }); } |
The details shown above will create a table for us containing the following fields:
id
– a unique identifier for every recorduser_id
– related to the id of the username
– the name of the projectdescription
– the project’s descriptiontimestamps
– the time stamp of creation for every record
We’re also going to modify the migration file so the user can use their user name to log in instead of their email address.
To do this, we’ll append the following code to the user migration file named 2014_10_12_000000_create_users_table.php
:
1 2 3 4 5 6 7 8 9 10 11 12 | public function up() { Schema::create('users', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('name'); $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); $table->string('password'); $table->rememberToken(); $table->timestamps(); }); } |
- We also need to modify the default string length. Let’s navigate to the
app
folder, then to theProviders
folder, where we’ll open up theAppServiceProvider.php
file. In that file, we’ll append the following details that are highlighted in the image:
Finally, we have to enable the PostgreSQL driver in our php.ini
file. Let’s go into our XAMPP directory and modify this file by commenting out these two drivers:
- extension=pdo_pgsql
- extension=pgsql
After all of these modifications, we’re ready to run the migration in the terminal. We’ll use the following command:
1 2 3 4 5 6 7 8 9 10 | C:\xampp\htdocs\projectmanager>php artisan migrate Migration table created successfully. Migrating: 2014_10_12_000000_create_users_table Migrated: 2014_10_12_000000_create_users_table (0.02 seconds) Migrating: 2014_10_12_100000_create_password_resets_table Migrated: 2014_10_12_100000_create_password_resets_table (0.01 seconds) Migrating: 2019_08_19_000000_create_failed_jobs_table Migrated: 2019_08_19_000000_create_failed_jobs_table (0.01 seconds) Migrating: 2019_11_13_070146_create_projects_table Migrated: 2019_11_13_070146_create_projects_table (0.01 seconds) |
We have successfully performed our Laravel migration.
Conclusion
Building a web app using both PostgreSQL and the Laravel PHP framework is a complex process that requires multiple steps. In our multi-part tutorial series, we walk you through every step of the process. This second installment of the series showed you how to create the model and controller components of the application. With the instructions and examples provided in this tutorial, you’ll have no problem building a Laravel PostgreSQL web app of your own.
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.
Get Started