Configuration for Laravel
Below is a list of all the configuration I usually do for all my Laravel (opens in a new tab) projects.
Eloquent "Strict Mode"
I like to use the strict mode for all models. This will give a benefit to me, because:
- It will prevent unexpected errors during local development when attempting to set an attribute that has not been added to the model's fillable array
- It will throw an exception if you attempt to access an attribute on a model when that attribute was not actually retrieved from the database or when the attribute does not exist on the model
- Disable lazy loading of relations
Below is an example of how to enable strict mode for all models, and additionally it will only run in the local environment.
use Illuminate\Database\Eloquent\Model;
/**
* Bootstrap any application services.
*/
public function boot(): void
{
Model::shouldBeStrict(! $this->app->isProduction());
}
Laravel Config
Timezone
By default, Laravel is using the UTC
timezone. I usually let it use the default timezone provided by Laravel.
This will prevent any issues with daylight savings time, and easily allow you to change the timezone of your application based on the user's location.
Database
I usually use the mysql
driver for my database.
For the engine
I usually use InnoDB
because it supports transactions and foreign keys.
To set the engine
for all tables, you can add the following to your config/database.php
file:
'mysql' => [
'engine' => 'InnoDB',
],
Logging
I prefer to use the daily
driver for logging. This will create a new log file for each day to make it easier to find the logs you are looking for.
'stack' => [
'driver' => 'stack',
'channels' => ['daily'],
'ignore_exceptions' => false,
],
If you need a real-time error tracking, you can use slack channel which is already supported by Laravel. To enable it, you need to add the following to your config/logging.php
file:
'stack' => [
'driver' => 'stack',
'channels' => ['daily', 'slack'],
'ignore_exceptions' => false,
],
Then you need to add the following to your .env
file:
LOG_SLACK_WEBHOOK_URL=https://hooks.slack.com/services/...
For configuring the slack channel, you can refer to the Laravel documentation (opens in a new tab).