Use a Closure with updateOrInsert() in Laravel 11.10
Published on by Paul Redmond
This week, the Laravel team released v11.10, with the ability to pass a callback to updateOrInsert
, support for soft-deleted models with explicit route model binding, and more.
updateOrInsert()
Allow Passing a Callback to Mark Eriksson contributed the ability to pass a callback as the second argument to the updateOrInsert()
query builder method. Within the callback, the boolean $exists
variable is passed so you can write logic to update specific columns based on whether the record exists or not:
DB::table('users')->updateOrInsert( ['user_id' => $user_id], function ($exists) use ($data) { if ($exists) { return [ 'name' => $data['name'], 'email' => $data['email'], ]; } return [ 'name' => $data['name'], 'email' => $data['email'], 'optional_column' => $data['foobar'], ]; });
See Pull Request #51566 for full implementation details.
Support Soft-deleted Models When Using Explicit Route Model Binding
Graham Bradley added support for soft-deleted models when using explicit route model binding:
This PR allows the resolution of soft-deleted models when using Laravel's explicit route-model binding feature.
It allows developers to use explicit route-model binding without having to customise the resolution logic when dealing with soft-deleted models. To do so it uses the same withTrashed() method as implicit binding.
// BeforeRoute::get('/users/{user}', ...);Route::bind('user', function (string $value) { return User::where('id', $value)->withTrashed()->firstOrFail();}); // AfterRoute::get('/users/{user}', ...)->withTrashed();Route::model('user', User::class);
See Pull Request #51651 for full implementation details.
Allow Setting Resend API Key in Mailer Config
@riasvdv contributed setting the Resend API key directly in the mailer config instead of just the services
config. See Pull Request #51618 for more details.
Release notes
You can see the complete list of new features and updates below and the diff between 11.9.0 and 11.10.0 on GitHub. The following release notes are directly from the changelog:
v11.10.0
- [11.x] Fix typo in filename by @Henridv in https://github.com/laravel/framework/pull/51643
- [11.x] Add Vite auto refresh to error page by @riasvdv in https://github.com/laravel/framework/pull/51635
- [11.x] Add test for join_paths by @imanghafoori1 in https://github.com/laravel/framework/pull/51621
- [11.x] Preload base options for missing config files by @jasonmccreary in https://github.com/laravel/framework/pull/51619
- [11.x] Add option to disable merging of base configuration by @taka-oyama in https://github.com/laravel/framework/pull/51579
- [11.x] Allow callback to be passed to
updateOrInsert()
to pass different$values
if the record already exists by @Markshall in https://github.com/laravel/framework/pull/51566 - [11.x] Fix
join_paths
issue with segment '0' by @imanghafoori1 in https://github.com/laravel/framework/pull/51649 - [11.x] Remove extra double quote in the error page by @nicolus in https://github.com/laravel/framework/pull/51670
- [11.x] Add tests to improve test coverage for
HtmlString
by @saMahmoudzadeh in https://github.com/laravel/framework/pull/51666 - [11.x] Add tests to improve test coverage for
Arr::whereNotNull
by @saMahmoudzadeh in https://github.com/laravel/framework/pull/51661 - [11.x] Add tests for FileSystem class by @imanghafoori1 in https://github.com/laravel/framework/pull/51654
- [11.x] Update OptimizeClearCommand.php by @nathanpurcell in https://github.com/laravel/framework/pull/51667
- [11.x] Support soft deleted models when using explicit route model binding by @gbradley in https://github.com/laravel/framework/pull/51651
- [11.x] Add tests for
Arr::divide
by @saMahmoudzadeh in https://github.com/laravel/framework/pull/51673 - [11.x] Prune should be a flag option by @riasvdv in https://github.com/laravel/framework/pull/51694
- [11.x] Avoid using Laravel new error page if
app.debug
changes totrue
at runtime by @crynobone in https://github.com/laravel/framework/pull/51705