Laravel Eloquent 检索包含空字段的关系的记录

Laravel Eloquent Retrieve Records Which Has Null Fields Included Relationship

我正在尝试获取具有空字段的关系记录。我的交易模式也是如此。

<?php

namespace App\Models;

class Deal extends Model
{
use HasFactory;
use SoftDeletes;

protected $fillable = [
    'id',
    'creator_user_id',
    'user_id',
    'owner_name',
    'person_id',
    'person_name',
    'stage_id',
    'title',
    'value',
    'currency',
    'formatted_value',
    'status',
    'won_time',
    'note',
    'source',
    'gender',
    'nationality',
    'city',
    'language',
    'passport_number',
    'arrival_airport',
    'arrival_flight_code',
    'arrival_date',
    'arrival_time',
    'hotel',
    'hotel_nights',
    'hotel_check_in_date',
    'hotel_check_out_date',
    'departure_airport',
    'departure_flight_code',
    'departure_date',
    'departure_time',
    'discount'
];

public function person()
{
    return $this->hasOne('App\Models\Person','id','person_id');
}

/**
 * Scope a query to all fields not filled.
 *
 * @param  \Illuminate\Database\Eloquent\Builder  $query
 * @return \Illuminate\Database\Eloquent\Builder
 */
public function scopeNotFilled($query): \Illuminate\Database\Eloquent\Builder
{
    return $query
        ->orWhereNull('source')
        ->orWhereNull('gender')
        ->orWhereNull('nationality')
        ->orWhereNull('city')
        ->orWhereNull('language')
        ->orWhereNull('arrival_airport')
        ->orWhereNull('arrival_flight_code')
        ->orWhereNull('arrival_date')
        ->orWhereNull('arrival_time')
        ->orWhereNull('hotel')
        ->orWhereNull('hotel_nights')
        ->orWhereNull('hotel_check_in_date')
        ->orWhereNull('hotel_check_out_date')
        ->orWhereNull('departure_airport')
        ->orWhereNull('departure_flight_code')
        ->orWhereNull('departure_date')
        ->orWhereNull('departure_time')
        ;
   }
}

当我尝试通过以下查询获取数据时

         return Deal::notFilled()->
         whereHas('person',function ($query){
            $query->orWhere('email',null);
         })
        ->where('won_time','>',Carbon::parse('2021-01-01'))
        ->orderBy('won_time', 'desc')
        ->paginate(15);

我可以获得有一些空白字段的交易,但它不包括没有 email 的关系。我想获得具有空字段的交易,并且如果关系没有 email 地址必须在列表中。我该如何解决这个问题?

我已经通过这样的连接解决了我的问题;

    return Deal::where('won_time','>',Carbon::parse('2021-01-01'))->
    join('people','people.id','=','deals.person_id')->
    where(function($q){
       $q->notFilled()
       ->orWhereNull('people.email');
    })->
    orderBy('won_time', 'desc')->
    select('deals.*')->
    paginate(15);

如果有任何有效的方法来解决这个问题,我可以听到你的解决方案。