LaravelをHerokuにデプロイする

# Laravelアプリケーションの作成
###Laravelプロジェクトの作成
```terminal
$ composer create-project laravel/laravel --prefer-dist hello_laravel_heroku
Installing laravel/laravel (v5.1.11)
  - Installing laravel/laravel (v5.1.11)
    Downloading: 100%

Created project in hello_laravel_heroku
...
...

$ cd hello_laravel_heroku
```
### Gitリポジトリの初期化
```terminal
$ git init
Initialized empty Git repository in ~/hello_laravel_heroku/.git/
$ git add .
$ git commit -m "new laravel project"
[master (root-commit) 6ae139d] new laravel project
 76 files changed, 5458 insertions(+)
...
```
#Herokuへのデプロイ
### Procfileの作成
```terminal
$ echo "web: vendor/bin/heroku-php-apache2 public/" > Procfile
$ git add .
$ git commit -m "Procfile for Heroku"
[master 1eb2be6] Procfile for Heroku
 1 file changed, 1 insertion(+)
 create mode 100644 Procfile
```
### herokuにログイン

```terminal
$ heroku login
heroku: Press any key to open up the browser to login or q to exit
 ›   Warning: If browser does not open, visit
 ›   https://cli-auth.heroku.com/auth/browser/***
heroku: Waiting for login...
Logging in... done
Logged in as me@example.com
```
### herokuに新しいアプリケーションを作成
```terminal
$ heroku create
Creating mighty-hamlet-1982... done, stack is cedar-14
http://mighty-hamlet-1982.herokuapp.com/ | git@heroku.com:mighty-hamlet-1982.git
Git remote heroku added
```

`mighty-hamlet-1982`がHerokuのアプリケーション名
###Laravelの暗号化キー設定
`php artisan key:generate --show`で見ることができる


```terminal
$ heroku config:set APP_KEY=…
Setting config vars and restarting mighty-hamlet-1982... done, v3
APP_KEY: ZEqur46KTEG91iWPhKGY42wtwi3rtkx2
```
###Herokuへpush

```terminal
$ git push heroku master
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 379 bytes | 0 bytes/s, done.
Total 4 (delta 3), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Fetching custom git buildpack... done
remote: -----> PHP app detected
remote: -----> Resolved 'composer.lock' requirement for PHP to version 5.6.14.
remote: -----> Installing system packages...
remote:        - PHP 5.6.14
remote:        - Apache 2.4.10
remote:        - Nginx 1.6.0
remote: -----> Installing PHP extensions...
remote:        - mbstring (composer.lock; bundled)
remote:        - zend-opcache (automatic; bundled)
remote: -----> Installing dependencies...
remote:        Composer version 1.0.0-alpha10 2015-04-14 21:18:51
remote:        Loading composer repositories with package information
remote:        Installing dependencies from lock file
...
remote:          - Installing laravel/framework (v5.1.19)
remote:            Downloading: 100%
remote:
remote:        Generating optimized autoload files
remote:        Generating optimized class loader
remote:        Compiling common classes
remote: -----> Preparing runtime environment...
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote:
remote: -----> Compressing... done, 74.5MB
remote: -----> Launching... done, v5
remote:        https://mighty-hamlet-1982.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/mighty-hamlet-1982.git
   1eb2be6..1b70999  master -> master
```
###サイトの表示
```terminal
$ heroku open
Opening mighty-hamlet-1982... done
```
# HerokuでPostgreSQLを使用する

```terminal
$ heroku addons:create heroku-postgresql:hobby-dev
Creating heroku-postgresql:hobby-dev on ⬢ sushi... free
Database has been created and is available
 ! This database is empty. If upgrading, you can transfer
 ! data from another database with pg:copy
Created postgresql-concave-52656 as DATABASE_URL
```
###LaravelとPostgreSQLの接続
Laravelの`config/database.php`ファイルを以下のように書き換える

```php:config/database.php
$DATABASE_URL = parse_url(getenv("DATABASE_URL"));

return [

    // …

    'connections' => [

        // …

        'pgsql' => [
            'driver' => 'pgsql',
            'host' => $DATABASE_URL["host"],
            'port' => $DATABASE_URL["port"],
            'database' => ltrim($DATABASE_URL["path"], "/"),
            'username' => $DATABASE_URL["user"],
            'password' => $DATABASE_URL["pass"],
            'charset' => 'utf8',
            'prefix' => '',
            'schema' => 'public',
            'sslmode' => 'require',
        ],

        // …

    ],

    // …

];
```
###Herokuにファイルを追加
```terminal
$ git add .
$ git commit -m "added postgres database configuration"
$ git push heroku master
```
###DBのマイグレーション
```terminal
$ heroku run php artisan migrate

 Do you really wish to run this command? (yes/no) [no]:
 > yes

Migration table created successfully.
Migrating: 2015_11_22_000000_create_users_table
Migrated:  2015_11_22_000000_create_users_table
Migrating: 2015_11_22_100000_create_password_resets_table
Migrated:  2015_11_22_100000_create_password_resets_table
```
#Herokuへのアップデート

herokuにソースコードやDBをpushしたい場合

```terminal
$ git add -A .
$ git commit -m "commit message"
$ git push heroku master
```

DBに変更があった場合

```terminal
$ heroku run php artisan migrate --seed
```