DEV Community

Cover image for Deploy Laravel application with database to Heroku

Deploy Laravel application with database to Heroku

Saf11000 on May 16, 2020

Why Heroku? I would like to start my blog with the question “why” as there are lots of technologies to choose from that are maybe better and mo...
Collapse
 
moghwan profile image
Marwane

Hi and thanks, this worked like a charm in laravel 7. just a quick note in database.php I changed only the default cnnection to pgsql and added DB_DATABASE as env variable in heroku dashboard. thanks again!

Collapse
 
safventure11000 profile image
Saf11000

Happy to help! :) and ohh I'll take note on the db settings. Thank you so much :)

Collapse
 
iamhabbeboy profile image
Azeez Abiodun Solomon

Thanks for sharing this 🤝, but instead of doing this

On your laravel app go to config/database.php file, and put this code on the top.

$DATABASE_URL=parse_url(‘DATABASE_URL’);
Change DATABASE_URL to the url on your heroku config.
Enter fullscreen mode Exit fullscreen mode

Why not add them to environment variable on Heroku, I guess that much better and neater. It works for me though ☺️.

Collapse
 
safventure11000 profile image
Saf11000 • Edited

Yep! Actually, this was mentioned in the other comments but I wasn't able to find time to update my blog. Now that you gave me this, I will go ahead and update it. Thanks for sharing.

Collapse
 
imanservisi profile image
Isabelle Manservisi

Thanks for this tutorial. It helped me a lot for my first deployment !

Some things to note :

  • with mySQL, it's free, even if heroku require to enter your credit card (no charge at all).
  • don't forget to go in your database Dashboard (in Heroku, click on your addon) and fill your .env with the correct values of host/user... For the password, put it in your heroku config var, so no one can catch it. And do the same in your database.php
Collapse
 
safventure11000 profile image
Saf11000

Nice! Happy to know that. And thank you for sharing your notes. :) Happy new year!

Collapse
 
beingwaqas profile image
beingwaqas

Hey,

When I run the migration command

heroku run php artisan migrate

I get the following error:

In Connection.php line 671:

SQLSTATE[22023]: Invalid parameter value: 7 ERROR: invalid value for param

eter "client_encoding": "utf8mb4" (SQL: select * from information_schema.ta

bles where table_schema = public and table_name = migrations and table_type

= 'BASE TABLE')

In PostgresConnector.php line 68:

SQLSTATE[22023]: Invalid parameter value: 7 ERROR: invalid value for param

eter "client_encoding": "utf8mb4"

Collapse
 
safventure11000 profile image
Saf11000 • Edited

hmmm. did you set pgsql as your default database? Alt Text

Collapse
 
mmimonir profile image
Md. Monirul Islam

Build Failed
show this message-
There are no commands defined in the "ide-helper" namespace.

   Script @php artisan ide-helper:generate handling the post-autoload-dump event returned with error code 1
Enter fullscreen mode Exit fullscreen mode

! WARNING: A post-autoload-dump script terminated with an error
! ERROR: Dependency installation failed!
!

! The 'composer install' process failed with an error. The cause
! may be the download or installation of packages, or a pre- or
! post-install hook (e.g. a 'post-install-cmd' item in 'scripts')
! in your 'composer.json'.
!

! Typical error cases are out-of-date or missing parts of code,
! timeouts when making external connections, or memory limits.
!

! Check the above error output closely to determine the cause of
! the problem, ensure the code you're pushing is functioning
! properly, and that all local changes are committed correctly.
!

! For more information on builds for PHP on Heroku, refer to
! devcenter.heroku.com/articles/php-...
!

! REMINDER: the following warnings were emitted during the build;
! check the details above, as they may be related to this error:
! - A post-autoload-dump script terminated with an error
! Push rejected, failed to compile PHP app.
! Push failed

Collapse
 
safventure11000 profile image
Saf11000

Hi,

Try these steps:

  1. Remove the line about "barryvdh/laravel-ide-helper" from require-dev on composer.json file.
  2. Then in your terminal execute : composer require barryvdh/laravel-ide-helper
Collapse
 
aldiazhar profile image
aldiazhar

Thank you for the tutor. When I tried the "git push heroku master" command. This error always appears.

Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi
In Connection.php line 671:
SQLSTATE[22023]: Invalid parameter value: 7 ERROR: invalid value for param

eter "client_encoding": "utf8mb4" (SQL: select * from "products" where "pro

ducts"."deleted_at" is null limit 8) In PostgresConnector.php line 68:

SQLSTATE[22023]: Invalid parameter value: 7 ERROR: invalid value for param

eter "client_encoding": "utf8mb4"

Collapse
 
safventure11000 profile image
Saf11000

I think you have the same error with @beingwaqas .. did you set pgsql as your default db?

Collapse
 
ellis22 profile image
Ellis

Nice article, straightforward and easy to understand.
I also suggest to watch this video as well: Deploy Laravel App on Heroku

Collapse
 
safventure11000 profile image
Saf11000 • Edited

Glad you liked it. :) I will check this video. Thanks.

Collapse
 
moatazabdalmageed profile image
Moataz Mohammady

Thanks but I want to install npm packages.

Collapse
 
jamols09 profile image
jamols09

I hope you could also create instructions for deploying two separate projects using AWS using laravel (backend) vuejs (frontend).

Collapse
 
njose profile image
Sebastian Njose

You're amazing! Thank u so muchh!! <3

Collapse
 
tareq12345 profile image
tareq12345

How to populate the heroku database with data on your local device

Collapse
 
safventure11000 profile image
Saf11000

hmmm.. I haven't tried that. But maybe this could help devcenter.heroku.com/articles/hero...

Collapse
 
didin1453fatih profile image
Didin 👨‍💻

I think you can combine Laravel and dbdesigner.id as your database designer. This can make your project readable and clear documentation.

Collapse
 
safventure11000 profile image
Saf11000

Thanks. i will look into that. :)

Collapse
 
mmimonir profile image
Md. Monirul Islam

Thanks a lot!
Create Procfile inside your main Laravel.
Where is the Profile file? In my local repo or heroku account?

Collapse
 
safventure11000 profile image
Saf11000 • Edited
Collapse
 
garvitjoshi profile image
Garvit Joshi

Hy, This was Really Informative .... Just a quick question: Should DATABASE_URL be kept secret?

Collapse
 
safventure11000 profile image
Saf11000 • Edited

Yes, you’re right. The suggestion of @marwane on the above comment is more appropriate. In database.php, just change the default connection to pgsql and the database is configured in heroku config vars. Sorry I have not updated my tutorial and thanks for pointing that out.