Laravel – condition on with()

  eloquent, laravel, laravel-8, php, query-builder

I wanted to join table NotificationTypes with UserEnabledNotificationTypesId on the NotificationTypesId column of UserEnabledNotifications , only for the rows in which deleted_at column of NotificationTypes table is null. I’ve tried as below. But it is not sorting for the null value of deleted_at column in NotifocationTypes table. Any solutions?

UserEnabledNotifications::with('userNotications', function ($query){
    $query->whereNull('deleted_at');
})->select('userId','notificationTypesId')->get();

UserEnabledNotifications

public function userNotifications(){
  return $this->belongsTo(NotificationTypes::class, 'notificationTypesId','id');
}

Source: Laravel

Leave a Reply