Laravel Eloquent LEFT JOIN WHERE NULL


Laravel Eloquent LEFT JOIN WHERE NULL

我正在尝试使用Eloquent在数据库种子期间执行以下查询:

SELECT
    *
FROM
    customers
LEFT JOIN
    orders
    ON customers.id = orders.customer_id
WHERE
    orders.customer_id IS NULL

下面是我在Eloquent中的实现:

$c = Customer::leftJoin('orders', function($join) {
      $join->on('customers.id', '=', 'orders.customer_id');
    })
    ->whereNull('orders.customer_id')
    ->first();

第一个查询总是返回完整的结果,而Eloquent等价查询总是为customers表的emailphone字段以外的所有字段返回空元素。我无法解释这一点,因为CustomersOrders模型都是手工生成的骨架。

例如:

class Customer extends 'Eloquent {
    // Add your validation rules here
    public static $rules = [
        // 'title' => 'required'
    ];
    // Don't forget to fill this array
    protected $fillable = [];
}

以下是当我对种子(最初由Faker生成)执行第一个Eloquent查询时输出的数组:

protected $original =>
  array(25) {
    'id' =>
    NULL
    'first_name' =>
    NULL
    'last_name' =>
    NULL
    'email' =>
    string(24) "luther.braun@example.org"
    'phone' =>
    string(17) "642.150.9176x5684"
    'address1' =>
    NULL
    'address2' =>
    NULL
    'city' =>
    NULL
    'state' =>
    NULL
    'county' =>
    NULL
    'district' =>
    NULL
    'postal_code' =>
    NULL
    'country' =>
    NULL
    'notes' =>
    NULL
    'created_at' =>
    NULL
    'updated_at' =>
    NULL
    'customer_id' =>
    NULL
    'total' =>
    NULL
}

这可以通过从特定表中指定所需的特定列名来解决,如下所示:

$c = Customer::leftJoin('orders', function($join) {
      $join->on('customers.id', '=', 'orders.customer_id');
    })
    ->whereNull('orders.customer_id')
    ->first([
        'customers.id',
        'customers.first_name',
        'customers.last_name',
        'customers.email',
        'customers.phone',
        'customers.address1',
        'customers.address2',
        'customers.city',
        'customers.state',
        'customers.county',
        'customers.district',
        'customers.postal_code',
        'customers.country'
    ]);

虽然其他答案很有效,但我想给你我经常使用的替代短版本

Customer::select('customers.*')
        ->leftJoin('orders', 'customers.id', '=', 'orders.customer_id')
        ->whereNull('orders.customer_id')->first();

laravel version 5.3一样,增加了一个功能,这将使您的工作更加简单,例如:

Customer::doesntHave('orders')->get();

您也可以在select中指定列,如下所示:

$c = Customer::select('*', DB::raw('customers.id AS id, customers.first_name AS first_name, customers.last_name AS last_name'))
->leftJoin('orders', function($join) {
  $join->on('customers.id', '=', 'orders.customer_id') 
})->whereNull('orders.customer_id')->first();

我会转储您的查询,这样您就可以查看实际执行的SQL,并了解它与您编写的内容有何不同。

您应该能够用以下代码做到这一点:

$queries = DB::getQueryLog();
$last_query = end($queries);
var_dump($last_query);
die();

希望这能给你足够的信息,让你找出问题所在。

我将使用laravelwhere DoesntHave来实现这一点。

Customer::whereDoesntHave('orders')->get();
use Illuminate'Database'Eloquent'Builder;
$query = Customers::with('orders');
$query = $query->whereHas('orders', function (Builder $query) use ($request) {
     $query = $query->where('orders.customer_id', 'NULL') 
});
    $query = $query->get();