alex.test/app/Console/Commands/AddBalance.php

46 lines
1005 B
PHP
Raw Normal View History

2024-09-17 14:50:06 +02:00
<?php
namespace App\Console\Commands;
use App\Models\User;
use Illuminate\Console\Command;
class AddBalance extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'user:add-balance {id} {balance}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Add balance to user';
/**
* Execute the console command.
*/
public function handle()
{
2024-09-17 15:03:29 +02:00
$balance = (double) $this->argument('balance');
2024-09-17 14:50:06 +02:00
$id = (int) $this->argument('id');
if ($balance == 0 || $id == 0) {
$this->fail('Balance or Id must be non zero value!');
}
/** @var User */
$user = User::find([ 'id' => $id ])->firstOrFail();
$user->balance += $balance;
$user->save();
$name = $user->name;
$balance = $user->balance;
$this->info("User $name has $balance balance now");
}
}