https://ohdear.app/news-and-updates/sql-performance-improvements-finding-the-right-queries-to-fix-part-1
- ❤️13
Product::query()
- ->with([
- 'attribute' => fn (Builder $query) => $query->select(['id', 'title'])
- ])
- ->whereHas('attribute');
+ ->withWhereHas('attribute:id,title') Product::query()
- ->whereHas('attribute', fn (Builder $query) => $query
- ->where('id', $id)
- );
+ ->whereRelation('attribute', 'id', $id) Product::query()
- ->with([
- 'attribute' => fn (Builder $query) => $query->select(['id', 'title'])
- ])
- ->whereHas('attribute', fn (Builder $query) => $query
- ->where('id', $id)
- );
+ ->with('attribute:id,title')
+ ->whereRelation('attribute', 'id', $id)