Laravel 8-具有多个条件的左外连接

Laravel 8-具有多个条件的左外连接,laravel,laravel-8,Laravel,Laravel 8,在我的Laravel应用程序的某些部分中,我需要使用左外连接获取数据。如果连接只需要一个条件,那么我可以很容易地处理这个问题(通过在Laravel join子句中添加left outer作为参数),但是我需要在left outer连接中使用两个条件。到目前为止,我编写了以下查询: $events = DB::table('events AS ev') ->join('event_registrations AS er', function ($joi

在我的Laravel应用程序的某些部分中,我需要使用左外连接获取数据。如果连接只需要一个条件,那么我可以很容易地处理这个问题(通过在Laravel join子句中添加left outer作为参数),但是我需要在left outer连接中使用两个条件。到目前为止,我编写了以下查询:

$events = DB::table('events AS ev')
                    ->join('event_registrations AS er', function ($join) {
                        $join->on('ev.id', '=', 'er.event_id')
                            ->where('er.status', '=', 'confirmed');
                    })
                    ->select('ev.id', 'ev.event_name', 'ev.event_link', 'ev.description', 'ev.total_tickets', 'ev.logo_path', DB::raw("IFNULL( count(er.id), 0 ) as total_confirmed"))
                    ->groupByRaw("ev.id, ev.event_name, ev.event_link, ev.description, ev.total_tickets, ev.logo_path, ev.total_tickets")
                    ->get();
这将创建一个内部联接查询。我已尝试按以下方式添加左外:

$events = DB::table('events AS ev')
                    ->join('event_registrations AS er', function ($join) {
                        $join->on('ev.id', '=', 'er.event_id')
                            ->where('er.status', '=', 'confirmed');
                    }, 'left outer')
                    ->select('ev.id', 'ev.event_name', 'ev.event_link', 'ev.description', 'ev.total_tickets', 'ev.logo_path', DB::raw("IFNULL( count(er.id), 0 ) as total_confirmed"))
                    ->groupByRaw("ev.id, ev.event_name, ev.event_link, ev.description, ev.total_tickets, ev.logo_path, ev.total_tickets")
                    ->get();
但它仍然会产生内部联接。

有人知道如何在Laravel中使用多个条件创建左外部联接查询吗?

如果您查看位于light\Database\query\Builder.php的源代码 join方法的定义如下

/**
 * Add a join clause to the query.
 *
 * @param  string  $table
 * @param  \Closure|string  $first
 * @param  string|null  $operator
 * @param  string|null  $second
 * @param  string  $type
 * @param  bool  $where
 * @return $this
 */
public function join($table, $first, $operator = null, $second = null, $type = 'inner', $where = false)
所以类型实际上是第五个参数,那么你的连接应该是

->join('event_registrations AS er', function ($join) {}, null, null, 'left outer')

您可以使用
leftJoin
,方法与使用
join