Laravel 关系 - 比较同一模型的两个 hasMany 关系

Laravel relationships - compare two hasMany relations of the same model

    $myOffers = Offer::where(function($offer) {
        $offer->where('sender_user_id', Auth::user()->id)->orWhere('receiver_user_id', Auth::user()->id);
    })->whereHasMorph('sender', \App\Models\Technique::class,
        function($sender) {

            
        // Technique has these relations: sentDeals, receivedDeals, sentOffers
        // Deal and Offer have datetimes start and end
        // I need to EXCLUDE all Techniques from myOffers list where any of Technique sentOffer items intersects with any of Technique sentDeals or receivedDeals (intersection by start and end dates, smth like:)
        // WHERE $myOffer->start <= $receivedDeal->end && $myOffer->end >= $receivedDeal->start

        }
    )->whereHasMorph('receiver', [\App\Models\ConstructionObjectAd::class, \App\Models\CargoAd::class],
        function($receiver) {
            $receiver->whereDoesntHave('sentDeals', function($sentDeals) {
                $sentDeals->whereIn('status', ['submitted', 'updated', 'confirmed', 'ended']);
            })->orWhere('sender_user_id', Auth::user()->id);
        }
    )->with(['sender', 'receiver'])->get();

技术具有以下关系:sentDeals、receivedDeals、sentOffers。 Deal 和 Offer 有日期时间开始和结束。我需要从 myOffers 列表中排除所有 Techniques,其中任何 Technique sentOffer 项目与任何 Technique sentDeals 或 receivedDeals 相交(按开始和结束日期相交,smth like:WHERE $myOffer->start <= $receivedDeal->end && $myOffer ->end >= $receivedDeal->start)。我怎样才能做到这一点?

看起来解决方案很简单

    $myOffers = Offer::where(function($offer) {
        $offer->where('sender_user_id', Auth::user()->id)->orWhere('receiver_user_id', Auth::user()->id);
    })->whereHasMorph('sender', \App\Models\Technique::class,
        function($sender) {
            $sender->whereDoesntHave('sentDeals', function($sentDeals) {
                $sentDeals->whereColumn('offers.start', '<=', 'deals.end')
                    ->whereColumn('offers.end', '>=', 'deals.start');
            })->whereDoesntHave('receivedDeals', function($receivedDeals) {
                $receivedDeals->whereColumn('offers.start', '<=', 'deals.end')
                    ->whereColumn('offers.end', '>=', 'deals.start');
            })->orWhere('sender_user_id', Auth::user()->id);
        }
    )->whereHasMorph('receiver', [\App\Models\ConstructionObjectAd::class, \App\Models\CargoAd::class],
        function($receiver) {
            $receiver->whereDoesntHave('sentDeals', function($sentDeals) {
                $sentDeals->whereIn('status', ['submitted', 'updated', 'confirmed', 'ended']);
            })->orWhere('sender_user_id', Auth::user()->id);
        }
    );