|
| 1 | +# How can we use this repo? |
| 2 | + |
| 3 | + |
| 4 | +# Install |
| 5 | +```sh |
| 6 | +composer install |
| 7 | +``` |
| 8 | +Next you can clone the `.env.example` file into the `.env` file. This means that you have to create the same file in the `root` directory under a different name e.g. `.env` and copy paste the same credentials like `.env.example` file. |
| 9 | + |
| 10 | +Laravel has a built-in CLI tool called `artisan`. Your application must generate a unique base 64 key that Laravel uses behind the scenes to bootstrap this project. |
| 11 | + |
| 12 | +**Command:** |
| 13 | + |
| 14 | +```sh |
| 15 | +php artisan key:generate |
| 16 | +``` |
| 17 | + |
| 18 | +It will automatically find your `.env` file and place the base 64 value in the file. |
| 19 | + |
| 20 | +**Output inside the file:** |
| 21 | +``` |
| 22 | +APP_KEY=base64:T0huMR5Wx9EoDmjTxniKTofHD/7cOiDeVVD9eTKuCa0= |
| 23 | +``` |
| 24 | + |
| 25 | +## Additional Note: |
| 26 | +As you can see it is necessary to create the `.env` file in your local to bootstrap the project. But `Laravel` contains 2 methods to connect to the database server. |
| 27 | + |
| 28 | +- Use of the `.env` variables *(I prefer this one)* |
| 29 | +- Use of the file located at the `config/database.php` |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | +## Use of the `.env` variables: |
| 34 | + |
| 35 | +When you create this file with copy paste credentials you can see default; the database variables are written something like this: |
| 36 | + ``` |
| 37 | +DB_CONNECTION=mysql |
| 38 | +DB_HOST=127.0.0.1 |
| 39 | +DB_PORT=3306 |
| 40 | +DB_DATABASE=test_app |
| 41 | +DB_USERNAME=root |
| 42 | +DB_PASSWORD= |
| 43 | +``` |
| 44 | + |
| 45 | +You can edit values according to your own database personal preference. I am using Postgres in this case. |
| 46 | + |
| 47 | +## Use of the file located at the `config/database.php` |
| 48 | + |
| 49 | +**Note:** When Laravel bootstraps the project it gives priority to the `.env` file as compared to `config/**` files. You can see `config/database.php` file contains an associated array with default database settings like this. |
| 50 | + |
| 51 | +``` |
| 52 | +return [ |
| 53 | + |
| 54 | + 'default' => env('DB_CONNECTION', 'mysql'), |
| 55 | + 'connections' => [ |
| 56 | + 'mysql' => [ |
| 57 | + 'driver' => 'mysql', |
| 58 | + 'url' => env('DATABASE_URL'), |
| 59 | + 'host' => env('DB_HOST', '127.0.0.1'), |
| 60 | + 'port' => env('DB_PORT', '3306'), |
| 61 | + 'database' => env('DB_DATABASE', 'forge'), |
| 62 | + 'username' => env('DB_USERNAME', 'forge'), |
| 63 | + 'password' => env('DB_PASSWORD', ''), |
| 64 | + 'unix_socket' => env('DB_SOCKET', ''), |
| 65 | + 'charset' => 'utf8mb4', |
| 66 | + 'collation' => 'utf8mb4_unicode_ci', |
| 67 | + 'prefix' => '', |
| 68 | + 'prefix_indexes' => true, |
| 69 | + 'strict' => true, |
| 70 | + 'engine' => null, |
| 71 | + 'options' => extension_loaded('pdo_mysql') ? array_filter([ |
| 72 | + PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'), |
| 73 | + ]) : [], |
| 74 | + ], |
| 75 | + ] |
| 76 | + ] |
| 77 | +``` |
| 78 | + |
| 79 | +You can only use these settings if variables from the `.env` file will be deleted. Otherwise, Laravel gives priority to `.env` variables. |
| 80 | + |
| 81 | +Delete the variables from the `.env`: |
| 82 | + |
| 83 | + |
| 84 | +~~DB_CONNECTION=mysql~~ |
| 85 | +~~DB_HOST=127.0.0.1~~ |
| 86 | +~~DB_PORT=3306~~ |
| 87 | +~~DB_DATABASE=test_app~~ |
| 88 | +~~DB_USERNAME=root~~ |
| 89 | +~~DB_PASSWORD=~~ |
| 90 | + |
| 91 | +Lastly, Update the `config/database.php` with your database server settings: |
| 92 | + |
| 93 | +``` |
| 94 | +'default' => env('DB_CONNECTION', 'pgsql') |
| 95 | +'database' => env('DB_DATABASE', 'lara8_api'), |
| 96 | +'username' => env('DB_USERNAME', 'postgres'), |
| 97 | +'password' => env('DB_PASSWORD', 'a') |
| 98 | +``` |
| 99 | + |
| 100 | +# Database |
| 101 | +I am using **Postgres** and inside `.env` file my database server credentials are: |
| 102 | +``` |
| 103 | +DB_CONNECTION=pgsql |
| 104 | +DB_HOST=127.0.0.1 |
| 105 | +DB_PORT=5432 |
| 106 | +DB_DATABASE=lara8_api |
| 107 | +DB_USERNAME=postgres |
| 108 | +DB_PASSWORD=a |
| 109 | +``` |
| 110 | + |
| 111 | +However, your main server and database server get started. |
| 112 | + |
| 113 | +# Migration (Transform into real database tables) |
| 114 | +At the last make sure after updating your database settings. Please use `artisan` CLI to migrate the database tables. |
| 115 | +```sh |
| 116 | +php artisan migrate |
| 117 | +``` |
| 118 | + |
| 119 | + |
| 120 | +# Deployment |
| 121 | +[Heroku](https://www.heroku.com) |
0 commit comments