Make use of the value method on the query builder

Make use of the value method on the query builder to execute a more efficient query when you only need to retrieve a single column.

1// Before (fetches all columns on the row)
2Statistic::where('user_id', 4)->first()->post_count;
3 
4// After (fetches only `post_count`)
5Statistic::where('user_id', 4)->value('post_count');

Tip given by @mattkingshott

Like our articles?

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