Photo by Cookie the Pom on Unsplash

Deploy Laravel App on Heroku for FREE with database

theoneamin
4 min readMay 30, 2021

--

In this tutorial, we are going to be looking at how to deploy a Laravel application on Heroku. Heroku is a great hosting platform, especially if you have a small application that maybe has low traffic or you just want a place to showcase your work, Heroku makes it super easy to do that. You can host your website completely for free. By the end of this tutorial, you will have your Laravel application live, running on the internet.

Create a simple Laravel application

First, we are going to create a new Laravel application. If you already have your application, you can skip this part.

Laravel new heroku-tutorial

Once we are done, we can cd into our app and then start the server

php artisan serve

Next, we add the database connection. In your .env, change the database name. You will need to have created a database beforehand.

Route::get()

In the routes file, I am going to map the home route to the home method in the controller class.

Route::get('/', 'Controller@home');

If you are using Laravel 8, and you find that the above is giving errors, it is because of namespaces. For this to work, we will need to edit the RouteServiceProvider.php file. Just uncomment the following line.

Then, we will create the home method in the controller and we are simply going to return the welcome view for now.

public function home() { 
return view('welcome');
}

Database

Now we are ready to populate the database. We will first run the default migration and then seed the database or you can do both at the same time.

You don’t have to do this but I will increase the number of records I want to 50.

php artisan migrate 
php artisan db:seed

Displaying the data

To display the data, I will get all the users from the database as shown below and pass them to the view. Make sure to import the User class.

public function home() { 
$users = User::paginate(10);
return view('welcome')->with([ 'users' => $users ]);
}

Then in the welcome view, I will simply use foreach to loop through the users and display each one of them. I have used tailwind tables template and here is the result.

Deployment

To deploy the application to Heroku, I have already pushed my code to a Github repo. Once done that, we can move to Heroku and log in to our dashboard.

Important:

For Heroku to know where to look for our files, we need to add a Procfile. In your root folder, where you have your .env and composer files, create a new file called Procfile without any extension and add the following to it. Once done, push your changes to GitHub.

web: vendor/bin/heroku-php-apache2 public/

Create a new app and select a region.

Then in the deployment tab, select GitHub as the method and also search and attach your repository.

Now before deploying, let’s add a database. Go to the overview tab, and install an addon. Search for MySQL and select clearDB.

Configuring environment variables

Let’s head over to the settings tab and click reveal config variables. You will see that clearDB has already added a URL for us. This URL has all information we need for our database.

You can add all the variables from your .env file if you wanted but you should have at least the following variables.

The values for the database connection can be gotten from the clearDB URL. The following illustrates where each can be found.

mysql --db_connection 
://
db_username
:
db_password
@
db_host
/
db_database
?
reconnect=true

We are almost done.

Now we can deploy our application. Go to the deploy tab and click deploy branch. Next, we will migrate the database. Click on more and then run console.

Run php artisan migrate and then php artisan db:seed. Once done visit the URL and your application up without any errors.

Thanks for reading, hope you found this helpful.

--

--