【发布时间】:2018-03-04 16:31:25
【问题描述】:
我有这样的关系
+------------+---------+
| id | problem |
+------------+---------+
| 1 | problem |
+------------+---------+
+------------+----------+---------+
| id | problemid|solution |
+------------+----------+---------+
| 1 | 1 |solution1|
+------------+----------+---------+
| 2 | 1 |solution2|
+------------+----------+---------+
我已经完成了连接,问题是当我在视图中执行 foreach 时,我得到 2 个具有相同问题的项目和 2 个不同的解决方案,但我需要得到相同的问题(一个问题)和 2 个解决方案。我该怎么做?
这是Controller中的代码:
$problem=Problem::select()
->join('solution','problem.id','=','solution.problemid')
->where('problem.id',$id)
->get();
【问题讨论】:
-
使用relationships 更优雅地做到这一点
-
@JiFus 我已经这样做了,但是我还有另一个问题,那就是我正在使用 join。
-
如果您的主要兴趣点是问题,以及相关的解决方案,我认为您应该在“problem.id”而不是“solution.id”的地方做。但显然,如果您只有一条记录有问题,那将没有什么区别。
-
当您在 where 条件下使用 solution.id 时,您如何获得两种不同的解决方案对您的问题的回答将在 where 条件下使用 problem.id
-
@mwal 我的错误,在我的查询中我有问题。我编辑它。
标签: php laravel laravel-5 foreach