init: commands
This commit is contained in:
parent
86c582d273
commit
9ac92b9890
|
@ -0,0 +1,45 @@
|
|||
<?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");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class MakeUser extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'make:user {name}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Create a new user';
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$name = (string) $this->argument('name');
|
||||
$user = new User();
|
||||
$user->name = $name;
|
||||
$user->save();
|
||||
$id = $user->id;
|
||||
$this->info("Created user with id $id");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class ShowUsers extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'show:users {page=1}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Show all users';
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$page = (int) $this->argument('page');
|
||||
$users = User::all()->forPage($page, 20)->all();
|
||||
|
||||
$this->info("Listing all users for page $page");
|
||||
$this->info("Id\tName\tBalance");
|
||||
foreach ($users as $user) {
|
||||
$this->info($user->id . ":\t" . $user->name . "\t" . $user->balance);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue