【发布时间】:2015-06-25 09:57:56
【问题描述】:
我正在使用 api 调用,您应该在其中搜索“调查”并总结不同的答案(位于其他表格中)。我是 eloquent 的新手,这对我来说有点复杂,所以我需要一些帮助。
有 4 个不同的数据表,Survey、SurveyAnswerSets、Survey_X、Survey_XY。
我想做的是使用标题、startDate 和 endDate 搜索调查。从获取的调查中,我需要从其他 3 个表格中获取更多信息。我想我应该使用急切加载。
这是我应该获取的数据字段:
"surveyId": Survey.surveyId
"title": Survey.title
"startDate": Survey.startDate
"endDate": Survey.endDate
"targetReplies": Survey.targetReplies
"repliesExposed": Number of rows in SurveyAnswerSets where surveyId = surveyId and isRefGroup = 0 and isComplete = 1
"repliesControl": Number of rows in from SurveyAnswerSets where surveyId = surveyId and isRefGroup = 1 and isComplete = 1
"repliesTotal": Number of rows in from SurveyAnswerSets where surveyId = surveyId and isComplete = 1
"useRefGroup": Survey.useRefGroup
"orderId": Survey.orderId
"surveyXIds": Survey_X where surveyId = surveyId
"originalXYIds": Survey_XY where surveyId = surveyId
选择我所做的正确调查的简单部分:
$columns = array('surveyId', 'title', 'startDate', 'endDate', 'isTemplate');
$surveys = Survey::notemplate()
->where('title', 'LIKE', "%$name%")
->where('startDate', '<=', Carbon::now()->toDateString())
->where('endDate', '>', Carbon::now()->toDateString())
->select($columns)
->get();
但这留下了我需要帮助或建议如何开始的棘手部分。
【问题讨论】:
-
如果您想在一次访问数据库中获得结果,我建议您使用原始 SQL 查询,而不是尝试使用 Eloquent 来表达它。