I am trying to return all models that does not have a specific relationship object. But the doesntHave method is returning nothing – even though there are plenty of models where a relationship does not exist.

Let me explain. Here is my product model (altered for simplicity)



namespace AppModels;

use ...

class Product extends Model
    use HasFactory, HasUuids;

    public function productPurchases(): HasMany
        return $this->hasMany(ProductPurchase::class);

    public function productType(): BelongsTo
        return $this->belongsTo(ProductType::class);

In my controller, I have the following query:


$product = Product::where('product_type_id', '=', $typeId)
    ->with(['productPurchases' => function($q) use ($clientId)
        $q->where('client_id', '=', $client->id);
    }, 'productType'])

This is yielding no results. So to debug, I removed the whereDoesntHave method to see what relationships for productPurchases are being returned. The result of one item I would expect to be returned is:

    "id": "ee882f50-1c6d-4ce8-9b18-f14a5f8e4474",
    "product_type_id": "9bb8bca4-67c8-4b7d-a8dc-6998fbbaad12",
    "product_purchases": [],
    "product_type": [
        "id": "9bb8bca4-67c8-4b7d-a8dc-6998fbbaad12",
        "name": "ABC",
        "status_id": "9bb8bca3-3b72-4583-9591-bd89f4d099de",
        "created_at": "2024-04-03T20:50:11.000000Z",
        "updated_at": "2024-04-03T20:50:11.000000Z",

so product_purchases is an empty array. How can I use doesntHave to return this result?