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

46 lines
1002 B
PHP

<?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()
{
$balance = (int) $this->argument('balance');
$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");
}
}