Commit 28b1a49dec3aca0e8f2976635a16eec124018956

Authored by Hayk Nazaryan
1 parent 2ea18676c7
Exists in master

delete old messages

Showing 1 changed file with 2 additions and 2 deletions Inline Diff

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