'What is a more efficient query for this many to many eloquent query?
There are the following tables: lists, items, and the pivot table items_to_lists, which has the columns list_id and items_id.
I know the list_id and account_id and I need to get all the items (with specific columns) that belong to it and belong to the account (there is accounts table),
I currently tried the following:
$items = Lists::with(['items' => function ($query) {
$query->select('column1', 'column2');
}])->find(3);
But it looks like it's not efficient because it also fetches the pivot table data with every item:
"items": [
{
"id": 1,
"name": "Item Name",
"pivot": {
"id"..
"list_id"..
"item_id"..
}
},
// ..
What query is better for this purpose? Is it more close to a raw SQL, or I can still make this eloquent efficient and choose only the needed data (without the pivot)?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
