Skip to content
Advertisement

How to know if my variable is empty (or null) after query – laravel

I am trying to do a permanent delete of previously deleted rows with softdeletes, but when there are no rows to delete my function stops working, I need an “if” for so that this does not happen

my function

public function handle()
    {
        $mailMessage = MailMessage::query()->whereNotNull('deleted_at');
        $mailMessage->onDelete();
        $mailTask = MailTask::query()->whereNotNull('deleted_at');
        $mailTask->onDelete();
        $meo = Meo::query()->whereNotNull('deleted_at');
        $meo->onDelete();
        $this->info('Display this on the screen');
    }

for example I’m trying to verify that the variable “$mailMessage” is not empty to continue with my function

thanks for read me 🙂

Advertisement

Answer

you can use simple delete:

public function handle()
{

  MailMessage::query()->whereNotNull('deleted_at')->delete();
  MailTask::query()->whereNotNull('deleted_at')->delete();
  Meo::query()->whereNotNull('deleted_at')->delete();
      
    $this->info('Display this on the screen');
}

added by author: replacing delete for forceDelete is working for me

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement