Blame view
app/Console/Commands/CreateAdminChat.php
1.5 KB
09a2fc9d8 messages and subs... |
1 2 3 4 5 |
<?php namespace App\Console\Commands; use App\Models\Chat; |
01a2f91b2 delete old messages |
6 |
use App\Models\Message; |
09a2fc9d8 messages and subs... |
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
use App\Models\User; use Illuminate\Console\Command; class CreateAdminChat extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'admin:chat'; /** * The console command description. * * @var string */ protected $description = 'Command description'; /** * Execute the console command. * * @return int */ public function handle() { |
01a2f91b2 delete old messages |
33 |
$superAdmin = User::superAdmin(); |
09a2fc9d8 messages and subs... |
34 |
|
01a2f91b2 delete old messages |
35 36 37 38 |
$users = User::where('id', '!=', $superAdmin->id)->get(); $chats = Chat::query() ->where('is_admin_chat', 1) ->orWhere('to_user_id', $superAdmin->id) |
09a2fc9d8 messages and subs... |
39 40 41 |
->get(); foreach ($chats as $chat) { |
01a2f91b2 delete old messages |
42 |
Message::query()->where('chat_id_from', $chat->id)->orWhere('chat_id_to', $chat->id)->delete(); |
09a2fc9d8 messages and subs... |
43 44 45 46 |
$chat->delete(); } $loopCount = $users->count(); |
01a2f91b2 delete old messages |
47 48 49 50 51 52 53 54 |
if ($superAdmin) { foreach ($users as $user) { Chat::firstOrCreate([ 'user_id' => $user->id, 'to_user_id' => $superAdmin->id, 'is_admin_chat' => 1, 'is_removed' => 0, ]); |
09a2fc9d8 messages and subs... |
55 |
|
01a2f91b2 delete old messages |
56 |
} |
09a2fc9d8 messages and subs... |
57 |
|
01a2f91b2 delete old messages |
58 |
$this->info("Admin chats created for {$loopCount} users."); |
09a2fc9d8 messages and subs... |
59 |
|
01a2f91b2 delete old messages |
60 61 |
return Command::SUCCESS; } |
09a2fc9d8 messages and subs... |
62 |
|
01a2f91b2 delete old messages |
63 |
$this->error("Admin is missing!"); |
09a2fc9d8 messages and subs... |
64 65 66 67 |
} } |