larastan icon indicating copy to clipboard operation
larastan copied to clipboard

Added the ability to check for unnecessary collection calls on dynamic properties in NoUnnecessaryCollectionCallRule

Open zlayaAvocado opened this issue 3 years ago • 0 comments

  • [X] Added or updated tests
  • [ ] Documented user facing changes
  • [X] Updated CHANGELOG.md

Changes

Added the ability to check for unnecessary collection calls on dynamic properties in NoUnnecessaryCollectionCallRule. E.g. in the following faulty example, instead of querying for all accounts for the user first, we build a query and execute it with the count

Bad: User::firstOrFail()->accounts->count();

Good: User::firstOrFail()->accounts()->count();

Also see example in this article why it's helpful and why it can cause performance issues.

Don’t use a collection to count the number of related entries.

Breaking changes

zlayaAvocado avatar Sep 19 '22 22:09 zlayaAvocado