我在模型中的这个功能:
NSArray *errors;
BOOL success = [self validate:&errors];
for (NSError *error in errors) {
NSLog(@"error %@", error);
}
正在输出:
public static function getUserQuestions($id_user = null, $sort_by = 'date')
{
$getData = DB::table('questions')
->select('map_user_question_vote.id_question, questions.id, SUM(map_user_question_vote.vote) as votes', false)
->leftJoin('map_user_question_vote', 'map_user_question_vote.id_question', '=', 'questions.id')
->where('questions.id_user', $id_user)
->where('map_user_question_vote.id_question', 'questions.id')
->groupBy('questions.id')
->get();
}
与此问题相同: mysql | Codeigniter Active Records are adding extra back ticks to query
但是对于Laravel而言,不是CI。
你还会注意到我的SUM()为......都搞砸了? 有没有办法解决这个问题?
答案 0 :(得分:2)
这样的事情怎么样:
$getData = DB::table('questions')
->select(DB::raw('map_user_question_vote.id_question, questions.id, SUM(map_user_question_vote.vote) as votes'))
->leftJoin('map_user_question_vote', 'map_user_question_vote.id_question', '=', 'questions.id')
->where('questions.id_user', $id_user)
->where('map_user_question_vote.id_question', 'questions.id')
->groupBy('questions.id')
->get();