Configure Laravel to use Immutable dates by default

September 29, 2023

By default, Laravel uses Carbon for dates, but the object returned by the \Illuminate\Support\Facades\Date helper is mutable.

To configure Laravel to use CarbonImmutable instead, call this method in your AppServiceProvider's boot() method:

 1use Carbon\CarbonImmutable;
 2use Illuminate\Support\Facades\Date;
 3
 4class AppServiceProvider extends ServiceProvider
 5{
 6    public function boot()
 7  	{
 8        Date::use(CarbonImmutable::class);
 9	}
10}

Just make sure to use Laravel's Date facade instead of Carbon directly in your application code.

MENU