Commit 2ea18676c7e2d8f4e3101a1af3eccf71613f30ab

Authored by Hayk Nazaryan
1 parent 09a2fc9d81
Exists in master

delete old messages

Showing 1 changed file with 47 additions and 0 deletions Side-by-side Diff

app/Console/Commands/DeleteOldMessages.php
... ... @@ -0,0 +1,47 @@
  1 +<?php
  2 +
  3 +namespace App\Console\Commands;
  4 +
  5 +use App\Models\Chat;
  6 +use App\Models\Message;
  7 +use App\Models\User;
  8 +use Illuminate\Console\Command;
  9 +
  10 +class DeleteOldMessages extends Command
  11 +{
  12 + /**
  13 + * The name and signature of the console command.
  14 + *
  15 + * @var string
  16 + */
  17 + protected $signature = 'app:delete-messages';
  18 +
  19 + /**
  20 + * The console command description.
  21 + *
  22 + * @var string
  23 + */
  24 + protected $description = 'Command description';
  25 +
  26 + /**
  27 + * Execute the console command.
  28 + *
  29 + * @return int
  30 + */
  31 + public function handle()
  32 + {
  33 + Message::query()->get()->map(function ($message) {
  34 + $chatFrom = Chat::query()->where(['id' => $message->chat_id_from])->first();
  35 + if (!$chatFrom) {
  36 + $chatFrom->delete();
  37 + }
  38 + $chatTo = Chat::query()->where(['id' => $message->chat_id_to])->first();
  39 + if (!$chatTo) {
  40 + $chatTo->delete();
  41 + }
  42 + });
  43 +
  44 +
  45 + return Command::SUCCESS;
  46 + }
  47 +}