Forwarded from The Dragon Code News
Часто используемые способы взаимодействия с релейшенами Laravel и их улучшение

1) Первый
 Product::query()
-    ->with([
-        'attribute' => fn (Builder $query) => $query->select(['id', 'title'])
-    ])
-    ->whereHas('attribute');
+    ->withWhereHas('attribute:id,title')


2) Второй
 Product::query()
-    ->whereHas('attribute', fn (Builder $query) => $query
-        ->where('id', $id)
-    );
+    ->whereRelation('attribute', 'id', $id)


3) Третий
 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)