【发布时间】:2019-01-16 01:06:36
【问题描述】:
我有一个查询(如下),我在编辑页面中获取产品规格列表,到目前为止它正在返回我的数据。问题是我无法获取他们父母的信息。
Query
$specs = DB::table('product_subspecification')
->where('product_id', '=', $product->id)
->join('subspecifications', 'subspecifications.id', '=', 'product_subspecification.subspecification_id')
->get();
Result
{#3106 ▼
+"id": 8
+"product_id": 15
+"subspecification_id": 8
+"title": "Xplorer 5500M"
+"specification_id": 6
+"status_id": 1
+"created_at": "2018-08-06 12:42:40"
+"updated_at": "2018-08-06 12:42:40"
}
问题
问题是 Xplorer 5500M 实际上是我的子规范(我将其保存为我的产品规范,it has parent 在这种情况下是 Keyboard 我还需要返回该父(键盘)名称。
所以稍后在我的刀片中,我会有类似的东西:
+------------+---------------+
| Parent | Specification |
+------------+---------------+
| Keyboard | Xplorer 5500M |
+------------+---------------+
Blade
@foreach($specs as $spacsdf)
<tr>
<td>Parent name here</td>
<td>{{$spacsdf->title}}</td>
<td>Del Button</td>
</tr>
@endforeach
PS:我尝试加入规格表(是父表名称) 但这次我得到的只是
Keyboard而不是Xplorer 5500M。
这是我尝试的第二个查询:
$specs = DB::table('product_subspecification')
->where('product_id', '=', $product->id)
->join('subspecifications', 'subspecifications.id', '=', 'product_subspecification.subspecification_id')
->join('specifications', 'specifications.id', '=', 'subspecifications.specification_id')
->get();
有什么想法吗?
【问题讨论】:
-
您想充分利用 Eloquent,请先阅读documentation on relations,然后在您自己尝试之后再回来询问。
-
@Kyslik 请仅在您对解决方案有想法的情况下发表评论,我与
x-editable有相同的方法,我的关系完美无缺,唯一的问题是我不想使用 x-editable在这个特定的部分,所以我应该以某种方式根据他们的 id 获取父名称,正如我在第二个查询中提到的那样。证明这是可能的,我已经做到了,请参阅ibb.co/feuZYz] -
首先我会随时发表评论,其次我建议你看看 Eloquent 而不是使用查询生成器。这可以在一行中完成(除了关系定义之外),最重要的是,任何精通 Eloquent 的人都会通过查看它来理解它。
-
谢谢老兄,我的问题已经解决了。
-
对你有好处;对在你之后阅读它的人不利。祝你好运!