Laravel 5.1 - DB正在向我的查询添加回形式?

时间:2015-07-10 14:46:25

标签: mysql laravel laravel-5

我在模型中的这个功能:

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()为......都搞砸了? 有没有办法解决这个问题?

1 个答案:

答案 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();