Did you know: affected rows after Eloquent update

Another quick tip - if you need to know, how many rows were actually affected if you launch Eloquent update() statement - it cannot be easier. So, for example, you launch something like this:
$products->whereNull('category_id')->update(['category_id' => 2]);
So how to find out how many products were actually without category before that update? The thing is that update() method returns that affected rows amount. You don't have to call any affected_rows or anything like that. Eloquent brings that directly to you:
$affected = $products->whereNull('category_id')->update(['category_id' => 2]);
echo "Affected rows: " . $affected;
Isn't Eloquent great?

No comments or questions yet...

Like our articles?

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

Recent Premium Tutorials