Jannik Baranczyk
Back to home
2024-11-04
The first thing I do in a fresh Laravel app
When I start a new Laravel app, the first thing I usually do is to add some opinionated configurations in the boot
method of AppServiceProvider.php
. I put each configuration into it’s own method, to keep it clean. The methods can then be called inside boot
.
1. Enable strict mode for Models
protected function configureModels(): void
{
Model::shouldBeStrict();
}
This does three things:
- Prevent lazy loading
- Prevent silently discarding attributes
- Prevent accessing missing attributes
2. Configure Commands
protected function configureCommands(): void
{
DB::prohibitDestructiveCommands(
$this->app->isProduction()
);
}
This prohibits destructive DB commands like db:wipe
, migrate:fresh
, migrate:refresh
, migrate:reset
etc. when the app is in production.
3. Configure Dates
use Carbon\CarbonImmutable;
use Illuminate\Support\Facades\Date;
// ...
protected function configureDates(): void
{
Date::use(CarbonImmutable::class);
}
Carbon instances are mutable objects by default, which can be a footgun. The above configuration enables immutable dates by default.
Be aware that you have to use the
Date
facade instead of theCarbon
class to make use of this.
The boot
method then calls each method:
use Carbon\CarbonImmutable;
use Illuminate\Support\Facades\Date;
// ...
public function boot(): void
{
// ...
$this->configureModels();
$this->configureCommands();
$this->configureDates();
}