【发布时间】:2018-12-03 06:00:26
【问题描述】:
不久前,我用 Larvel 开始了我的第一个项目,并在我的 Elequent 查询中遇到了这个奇怪的问题。
所以我有 3 张桌子:
|---------------------|------------------|------------------|
| storage | items | storage_contents |
|---------------------|------------------|------------------|
| id | id | id |
|---------------------|------------------|------------------|
| user_id | name | storage_id |
|---------------------|------------------|------------------|
| name | description | item_id |
|---------------------|------------------|------------------|
| description | kcal | amount |
|---------------------|------------------|------------------|
| | img_url | expiry_date |
|---------------------|------------------|------------------|
| | always_on_list | |
|---------------------|------------------|------------------|
storages 属于用户,storages 包含项目,storage_contents 是这两者之间的数据透视表。
如果有人想知道为什么我不只是将 expiry_date 和 amount 放在 Items 表中,而是放在他们自己的数据透视表中,那是因为我打算这样做,以便用户可以使用彼此的项目他们自己的仓库。例如,如果一位用户创建了 Apple 项目。当另一个用户开始输入时,他们会看到一个下拉菜单,其中包含从其他用户那里选择先前创建的 Apple 项目的选项。但是,虽然名称、描述、图像和千卡可能相同,但数量和到期日期可能因用户而异。因此我把它放在了一个数据透视表中。
在我尝试运行这个 Eloquent 查询之前一切正常:
$item = Items::find($id)->storageContent()->get();
storageContent() 是 Items 模型中的 Eloquent 关系:
public function storageContent(){
return $this
->belongsToMany(Items::class,'storage_contents','item_id','storage_id')
->withPivot('amount','expiry_date');
}
但是由于某种原因,当我执行这个查询时,我只从第一个表条目中获取数据(不管我在查询中输入了什么 ID),但是数据透视表的数据是来自正确的数据透视表。但我终其一生都无法弄清楚为什么会发生这种情况。
澄清我得到什么以及这个查询期望得到什么:
当我将查询放入 Artisan tinker 时:
假设我的 Items 表中有 2 个项目:
|-------------------|-------------------|
| items | items |
|-------------------|-------------------|
| id: 1 | id: 2 |
|-------------------|-------------------|
| name: foo | name: bar |
|-------------------|-------------------|
| description: null | description: null |
|-------------------|-------------------|
| kcal: null | kcal: null |
|-------------------|-------------------|
| img_url: null | img_url: null |
|-------------------|-------------------|
| always_on_list: 0 | always_on_list: 0 |
|-------------------|-------------------|
其中 foo 的数量为 123,bar 的数量为 321。
当我在 Artisan tinker 中输入 $item = Items::find(1)->storageContent()->get(); 时,我得到:
Illuminate\Database\Eloquent\Collection {#109
all: [
App\Items {#2337
id: 1,
name: "test1",
description: "foo",
kcal: null,
img_url: null,
always_on_list: 0,
created_at: "2018-06-24 15:12:03",
updated_at: "2018-06-24 15:12:03",
pivot: Illuminate\Database\Eloquent\Relations\Pivot {#2332
item_id: 1,
storage_id: 1,
amount: 123,
expiry_date: null,
},
},
],
}
这符合我期望得到的结果,但是当我这样做 $item = Items::find(2)->storageContent()->get(); 时,我得到:
Illuminate\Database\Eloquent\Collection {#109
all: [
App\Items {#2337
id: 1,
name: "test1",
description: "foo",
kcal: null,
img_url: null,
always_on_list: 0,
created_at: "2018-06-24 15:12:03",
updated_at: "2018-06-24 15:12:03",
pivot: Illuminate\Database\Eloquent\Relations\Pivot {#2332
item_id: 2,
storage_id: 1,
amount: 321,
expiry_date: null,
},
},
],
}
这不是我期望得到的,我期望得到:
Illuminate\Database\Eloquent\Collection {#109
all: [
App\Items {#2337
id: 2,
name: "test2",
description: "bar",
kcal: null,
img_url: null,
always_on_list: 0,
created_at: "2018-06-24 15:12:03",
updated_at: "2018-06-24 15:12:03",
pivot: Illuminate\Database\Eloquent\Relations\Pivot {#2332
item_id: 2,
storage_id: 1,
amount: 321,
expiry_date: null,
},
},
],
}
也许你们中的任何人都知道我做错了什么?
【问题讨论】:
-
什么是右数据透视表?如果您正在谈论我雄辩的术语,请告诉我表格名称或型号名称......不要指明表格的方向。请明确说明从当前查询中得到什么以及您期望什么?
-
@Ruman 对不起,我添加了一个示例,说明我期望得到什么以及我实际得到什么。