spebon's avatar

WHERE !(...) in Eloquent

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.

0 likes
3 replies
spebon's avatar

But that only applies to a single relationship. What I need here is for the conditions to be grouped inside the negation.

Please or to participate in this conversation.