testapi/app/Console/Commands/rmapiadmin.php

43 lines
1.1 KiB
PHP

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\User;
use App\Models\ApiAdmins;
class RmApiAdmin extends Command
{
protected $signature = 'rmapiadmin';
protected $description = 'List users with an ApiAdmins record and remove the relationship';
public function handle() {
$users = User::has('apiAdmin')
->orderBy('id')
->get(['id', 'name', 'email']);
if ($users->isEmpty()) {
$this->info('No users currently assigned as API Admin.');
return 0;
}
$choices = $users->map(fn($u) => "{$u->id}: {$u->name} ({$u->email})")->toArray();
$selected = $this->choice(
'Select a user to remove from API Admins',
$choices,
0,
null,
false
);
[$idString] = explode(':', $selected, 2);
$userId = (int) trim($idString);
ApiAdmins::where('user_id', $userId)->delete();
$this->info("User ID {$userId} has been removed from API Admins.");
return 0;
}
}