Laravel 9.25 released with new features list of string method, mass updating model timestamps with the query builder, and more. Let’s check these updates here:
1. whenNotExactly method() to Stringable :
Anjorin Damilare contributed a whenNotExactly
string method which will execute a given callback if the string is not an exact match with the given value. Here is the method detail.
/** * Execute the given callback if the string is not an exact match with the given value. * * @param string $value * @param callable $callback * @param callable|null $default * @return static */ public function whenNotExactly($value, $callback, $default = null) { return $this->when(! $this->exactly($value), $callback, $default); }
Now, Let’s see how we can use this with an example code:
First, with only the $value and the $callback function
Str::of('Tony') ->whenNotExactly('Tony Stark', function ($stringable) { return 'Iron Man'; })); // Returns `Iron Man` Because the value `Tony` is not the exact match with the `Tony Stark`
Second, with the default $value, so that if `false`, then it might return the default value.
// Provide an optional default value if `false` Str::of('Tony Stark') ->whenNotExactly('Tony Stark', function ($stringable) { return 'Iron Man'; }, function ($stringable) { return 'Swing and a miss...!'; })); // Returns `Swing and a miss...!`
2. Model::query()->touch() to mass update timestamps
Steve Bauman contributed a touch()
method to the model query builder that allows to touch the model’s timestamp with or without query constraints. it works like Model::touch()
. Here is the method detail:
/** * Update the column's update timestamp. * * @param string|null $column * @return int|false */ public function touch($column = null) { $time = $this->model->freshTimestamp(); if ($column) { return $this->toBase()->update([$column => $time]); } $column = $this->model->getUpdatedAtColumn(); if (! $this->model->usesTimestamps() || is_null($column)) { return false; } return $this->toBase()->update([$column => $time]); }
Now, Let’s see how we can use this with an example code:
// Mass updating the updated_at column using touch() User::query()->touch(); // Mass updating the updated_at column with query constraints User::where('email', 'like', '%@company.com')->touch(); // Mass updating a using specific column Post::query()->touch('published_at');
Now the list of the Laravel 9.25 Released updates:
Added
- Added whenNotExactly to Stringable (#43700)
- Added ability to Model::query()->touch() to mass update timestamps (#43665)
Fixed
- Prevent error in db/model commands when using unsupported columns (#43635)
- Fixes ensureDependenciesExist runtime error (#43626)
- Null value for auto-cast field caused deprication warning in php 8.1 (#43706)
- db:table command properly handle table who doesn’t exist (#43669)
Changed
- Handle assoc mode within db commands (#43636)
- Allow chunkById on Arrays, as well as Models (#43666)
- Allow for int value parameters to whereMonth() and whereDay() (#43668)
- Cleaning up old if-else statement (#43712)
- Ensure correct ‘integrity’ value is used for css assets (#43714)
To get to know more about the Laravel, you can check these articles too.