对于这个多对多 eloquent 查询,什么是更有效的查询?
What is a more efficient query for this many to many eloquent query?
有以下 tables:lists
、items
和主元 table items_to_lists
,它有列 list_id
和 items_id
.
我知道 list_id
和 account_id
,我需要获取属于它和属于 account
的所有 items
(具有特定列)(有 accounts
table),
我目前尝试了以下方法:
$items = Lists::with(['items' => function ($query) {
$query->select('column1', 'column2');
}])->find(3);
但它看起来效率不高,因为它还会获取每个项目的枢轴 table 数据:
"items": [
{
"id": 1,
"name": "Item Name",
"pivot": {
"id"..
"list_id"..
"item_id"..
}
},
// ..
什么查询更适合这个目的?它更接近原始 SQL,还是我仍然可以使这个 eloquent 高效并只选择需要的数据(没有数据透视)?
Eloquent 必须有数据透视表。如果从所选列中删除 topic_id
,则不会返回任何结果,因为它关联数据的方式。
假设您有 items_to_lists
的枢轴模型,您可以在 List 和 ItemToList
之间创建 HasMany
关系(或者您想要调用该模型),然后自定义查询,以便它加入您的 Item
模型。
class ItemToList extends Pivot
{
protected $table = 'items_to_lists';
}
class List extends Model
{
...
public function item_to_list()
{
return $this->hasMany(ItemToList::class, 'item_id');
}
}
$items = Lists::query()
->with(['item_to_list' => function ($query) {
$query->select('i.name', 'list_id', 'item_id')
->join('items as i', 'i.id', 'item_id');
}])
->find(3);
为了进行比较,您的原始查询创建了以下 SQL 个查询:
-- Query 1
select ...
from "lists" where "lists"."id" = 3
limit 1
-- Query 2
select
"items"."name",
"items_to_lists"."list_id" as "pivot_list_id",
"items_to_lists"."item_id" as "pivot_item_id"
from "items"
inner join "items_to_lists" on "items"."id" = "items_to_lists"."item_id"
where "items_to_lists"."topic_id" in (3)"
而我编写的替代方案会生成以下查询
-- Query 1
select ...
from "lists" where "lists"."id" = 3
limit 1
-- Query 2
select
"i"."name",
"list_id",
"item_id"
from "items_to_lists"
inner join "items" as "i" on "item_id" = "i"."id"
where "items_to_lists"."list_id" in (3)
如您所见,查询几乎相同。
有以下 tables:lists
、items
和主元 table items_to_lists
,它有列 list_id
和 items_id
.
我知道 list_id
和 account_id
,我需要获取属于它和属于 account
的所有 items
(具有特定列)(有 accounts
table),
我目前尝试了以下方法:
$items = Lists::with(['items' => function ($query) {
$query->select('column1', 'column2');
}])->find(3);
但它看起来效率不高,因为它还会获取每个项目的枢轴 table 数据:
"items": [
{
"id": 1,
"name": "Item Name",
"pivot": {
"id"..
"list_id"..
"item_id"..
}
},
// ..
什么查询更适合这个目的?它更接近原始 SQL,还是我仍然可以使这个 eloquent 高效并只选择需要的数据(没有数据透视)?
Eloquent 必须有数据透视表。如果从所选列中删除 topic_id
,则不会返回任何结果,因为它关联数据的方式。
假设您有 items_to_lists
的枢轴模型,您可以在 List 和 ItemToList
之间创建 HasMany
关系(或者您想要调用该模型),然后自定义查询,以便它加入您的 Item
模型。
class ItemToList extends Pivot
{
protected $table = 'items_to_lists';
}
class List extends Model
{
...
public function item_to_list()
{
return $this->hasMany(ItemToList::class, 'item_id');
}
}
$items = Lists::query()
->with(['item_to_list' => function ($query) {
$query->select('i.name', 'list_id', 'item_id')
->join('items as i', 'i.id', 'item_id');
}])
->find(3);
为了进行比较,您的原始查询创建了以下 SQL 个查询:
-- Query 1
select ...
from "lists" where "lists"."id" = 3
limit 1
-- Query 2
select
"items"."name",
"items_to_lists"."list_id" as "pivot_list_id",
"items_to_lists"."item_id" as "pivot_item_id"
from "items"
inner join "items_to_lists" on "items"."id" = "items_to_lists"."item_id"
where "items_to_lists"."topic_id" in (3)"
而我编写的替代方案会生成以下查询
-- Query 1
select ...
from "lists" where "lists"."id" = 3
limit 1
-- Query 2
select
"i"."name",
"list_id",
"item_id"
from "items_to_lists"
inner join "items" as "i" on "item_id" = "i"."id"
where "items_to_lists"."list_id" in (3)
如您所见,查询几乎相同。