基于关系的雄辩查询条件

时间:2017-10-27 12:25:42

标签: php laravel eloquent

Results(game_id, score) 
Games(start, end, threshold)

给出时间戳: 我想找到所有属于游戏的结果,并带有一个开始和放大器。结束时间包含时间戳时间和结果分数高于游戏阈值的位置。

我已设法查询时间条件,但如何另外查询Results.score > games.threshold

Results::whereHas('game', function($q) use ($timestamp) {
    $q->where('start', '<=', $timestamp)
      ->where('end', '>=', $timestamp)
})->with('game')->get()

1 个答案:

答案 0 :(得分:3)

尝试在whereHas中使用whereRaw并检查它是否有效:

Results::whereHas('game', function($q) use ($timestamp) {
    $q->where('start', '<=', $timestamp)
      ->where('end', '>=', $timestamp)
      ->whereRaw('results.score > games.threshold');
})->with('game')->get();
相关问题