New in Laravel 9.25: touch() on Query Builder

In one of the recent Laravel versions, they released an improvement to use the touch() method on Query Builder, and not only on Eloquent Model.

But, maybe some of you don't know what the touch() method does.

Generally, it is used if you don't want to update any data on your model, just save the fact that it was updated:

$user = User::find($id);
$user->touch();

It will be the same as doing:

$user = User::find($id);
$user->updated_at = now();
$user->save();

You can also use touch() on any timestamp field, like $user->touch('activated_at');

Now, in Laravel 9.25, Steve Bauman suggested a PR to add the same functionality to the Query Builder, so you would be able to "mass-touch" many models, in one sentence:

User::where('email', 'like', '%@company.com')->touch();

It would do the same update in the database, just by your query, and not a specific single model.

No comments or questions yet...

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 59 courses (1056 lessons, total 42 h 44 min)
  • 78 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord

Recent Premium Tutorials