Deploy Laravel application with a database to Heroku

Prerequisites

PHP Laravel knowledge.

A Heroku user account.

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

Create a Laravel Application

The 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

or

then

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:

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 a subdirectory, so you need to create a to configure the correct document root. This file must be created at the root of the project directory, the name with uppercase P.

Login to Heroku CLI

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

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 command:

It will create an application with a random name.

Now deploy this project on Heroku by pushing the source

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
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
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:

From the output note down

Open file, and put this code on the top, and set as default database like this:

config/detabase.php

And in the database connection set as below:

config/database.php

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

Now migrate the database, to do so, run:

or

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

If you want to seed the database:

It might give an error that during seeding. To overcome this go to file and cut from and paste it in 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

Software Developer @ mditech.net

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store