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

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

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

--

--

--

Software Developer @ mditech.net

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Cross Platforming: Why it Should be Important to You

Library Enhancements, Request Games, Auto Upload Changes & More!

Back to the fundamentals: Sorting algorithms in Swift (from scratch!)

Java vs. Other Programming Languages (Python, Ruby, C++): Does Java Come Out on Top?

The Lost Year — 2020 in review

What Is Construction Accounting Software?

DFK CV JEWEL-xJEWEL LP How it works?

Unable To Install MongoDB Locally

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
Madhavendra Dutt

Madhavendra Dutt

Software Developer @ mditech.net

More from Medium

Setup a LEMP Stack on Ubuntu 22.04

Create CRUD API using PHP and MySQL, Part-1

BEGINNERS GUIDE TO LARAVEL 9 LUMEN : HOW TO CREATE RESTful APIs WITH LUMEN 9

How to Clone Laravel Project from Git Repository and Run it on Local Machine