【发布时间】:2021-03-30 10:31:01
【问题描述】:
我不确定这是 laravel 中的错误还是我做错了什么。
我有两张桌子clothes 和clothes_category。
我拥有的 SQL 非常简单,我从衣服中选择所有内容并加入衣服类别,其中衣服.category_id 与衣服类别.id 映射。这应该只返回已经映射到clothing_categor.id 的衣服。简单易行。
Laravel 出于某种奇怪的原因而不是返回衣服 ID。它返回了clothing_category id,因此在衣服中找到的所有数据都与衣服_category.id一起返回。
返回的数据:
[
{
"id":1,
"name":"clothes 1", NOTICE this shows that the clothes data is actually different
"category_id":1,
"category":"hoodie",
},
{
"id":1,
"name":"clothes10", NOTICE this shows that the clothes data is actually different
"category_id":1,
"category":"hoodie",
}
]
Laravel 查询
return Clothes::join('clothes_category', 'clothes_category.id', '=', 'clothes.category_id')
->where('clothes_category.category', $cat)->get();
使用普通 SQL:
SELECT *
FROM clothes
INNER JOIN clothes_category ON clothes_category.id = clothes.category_id
WHERE clothes_category.category = 'hoodie';
[
{
"id":1, NOTICE id is different now
"name":"clothes 1",
"category_id":1,
"category":"hoodie",
},
{
"id":2, NOTICE id is different now
"name":"clothes10",
"category_id":1,
"category":"hoodie",
}
]
我不明白为什么会这样。有谁知道为什么使用 laravel 会发生这种情况?
【问题讨论】: