Deploy Laravel application with a database to Heroku

Madhavendra Dutt
4 min readDec 23, 2020

--

Prerequisites

PHP Laravel knowledge.

A Heroku user account.

Heroku CLI, Composer, and Git installed on your computer.

Create a Laravel Application

The composer create-project command is one of the ways you can bootstrap a new project based on the laravel/laravel. However, I am using https://github.com/mdutt247/laravel-news which is having an Admin dashboard on the web and RESTful API for consumers in Laravel 8 — Jetstream, Livewire, Sanctum, and Tailwind.

Open the project in command prompt and type

git clone https://github.com/mdutt247/laravel-news.git

or

composer create-project laravel/laravel laravel-news

then

cd laravel-news

Assuming this project is running in your local development environment and you are at the root directory of the project.

Initialize a Git repository

It’s now time to initialize a Git repository and commit the current state, to do so run the following commands:

git init

git add .

git commit -m “first commit”

Create Procfile file

Heroku will launch an Apache/Nginx web server together with PHP to serve applications from the root directory of the project.

Laravel application’s document root is public/ a subdirectory, so you need to create a Procfile to configure the correct document root. This file must be created at the root of the project directory, the name with uppercase P.

echo “web: vendor/bin/heroku-php-apache2 public/” > Procfile

git add .

git commit -m “Procfile for Heroku”

Login to Heroku CLI

Now you need to log into Heroku to push the project. Type following command:

heroku login

Then complete the login procedure by clicking the log in link.

Create a new application on Heroku

To create a new Heroku application that you can push to, use the heroku create command:

heroku create

It will create an application with a random name.

Now deploy this project on Heroku by pushing the source

git push heroku master

After this step, you can go to the Heroku dashboard and open the application, but this will give you an error as we have not set the environmental variable. To set environment variables open the dashboard and fill in the following keys, except DATABASE_URL, for now:

heroku config vars
Heroku Config Vars

When these variables are set you are ready to go.

Add Database

You can add any database of choice. However, I am using PostgreSQL because it is available free.

On the Heroku dashboard click on the Resources tab. Then search for Postgres:

add database
Add database

Select and install Heroku Postgres. After successful installation, Heroku Postgres is available in dashboard resources:

Dashboard — Resource

Now come back to your terminal and type:

heroku config

From the output note down DATABASE_URL

Open config/database.php file, and put this code on the top, and set pgsql as default database like this:

config/detabase.php

And in the database connection set pgsql as below:

config/database.php

Now we are all set to push these modifications. Run:

git add .

git commit –m “Updated database connection”

git push heroku master

Now migrate the database, to do so, run:

heroku run php artisan migrate

or

heroku run bash
php artisan migrate

If during migration you are getting error migrating laravel/telescope files. You may remove it:

composer remove laravel/telescope

If you want to seed the database:

heroku run php artisan db:seed

It might give an error that faker class not found during seeding. To overcome this go to composer.json file and cut fakerphp/faker from require-dev and paste it in require and push the code again to Heroku.

If everything works fine you will be accessing your Laravel application online.

Connect: Twitter, GitHub, Linkedin, MDITech

You can support by buying a coffee ☕️ here https://www.buymeacoffee.com/mdutt

--

--

No responses yet