Skip to main content
Tutorial Free

Did you know: affected rows after Eloquent update

July 03, 2015
1 min read
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?

Enjoyed This Tutorial?

Get access to all premium tutorials, video and text courses, and exclusive Laravel resources. Join our community of 10,000+ developers.

Recent Courses on Laravel Daily

Roles and Permissions in Laravel 13

14 lessons
57 min

Testing in Laravel 13 For Beginners

26 lessons
1 h 41 min read

Queues in Laravel 13

18 lessons
1 h 12 min read

No comments yet…