Laravel 左加入条件为 ON

Laravel Left Join with ON condition

我正在将查询转换为 Laravel 查询生成器。

" LEFT JOIN grade_level AS f ON (a.customer_type = 1 AND (e.id IS NULL AND d.grade_level_id = f.grade_level_id) OR (e.id IS NOT NULL AND e.grade_level_id = f.grade_level_id) ) "

我在查询过程中遇到 AND 和 OR 条件问题。

->leftJoin("grade_level", function ($join) {
  $join->on("transaction_db.customer_type" , "=", 1);   
  $join->on("enrollment.id", "=", "");
  $join->on("student.grade_level_id", "=","grade_level.grade_level_id");
  $join->orOn("enrollment.id", "=" "", "enrollment.grade_level_id", "=", "grade_level.grade_level_id");
}) 

如果其他方法都不起作用,您仍然可以执行以下操作:

->leftJoin("grade_level", function ($join) {
  $join->on(DB::raw("(a.customer_type = 1 AND (e.id IS NULL AND d.grade_level_id = f.grade_level_id) OR (e.id IS NOT NULL AND e.grade_level_id = f.grade_level_id) )");
}) 

我只是想分享一下我们在这里解决了问题。

$transactions->where(function($query) use ($glid) {
         $query->where(DB::raw("transaction_db.customer_type"), "!=", 1)
               ->orWhere(function($query1) use ($glid) {
                $query1->where(DB::raw("transaction_db.customer_type"), 1)
                       ->where("enrollment.grade_level_id", "=", $glid);
         });
});