Level 73
whereDoesntHave: https://laravel.com/docs/9.x/eloquent-relationships#querying-relationship-absence
as long as your relationships are correct.
I have the following snippet in an SQL WHERE clause:
!(
loggable_type='App\\Models\\Activity' AND
a.user_id<=>NULL AND
lifecycle_entries.event='created'
)
And want to replicate it in Eloquent. I tried this:
whereNot(function(Builder $query) {
$query->
whereHasMorph('loggable', [\App\Models\Activity::class], function(EloquentBuilder $query) {
$query->whereNull('activities.user_id');
})->
whereHasMorph('writableChange', [\App\Models\LifecycleChange::class], function(EloquentBuilder $query) {
$query->where('lifecycle_entries.event', 'created');
});
})
But whereNot is not the Eloquent way as it interprets the Not as a column name. Since the conditions are non-trivial I'd like to avoid a whereRaw method and use the proper ones instead. Is this possible? I can't find any mention of an Eloquent method for a negated WHERE clause.
Please or to participate in this conversation.