Laravel 5
Создание консольной команды в Laravel 5.5
1) Создаем класс для команды php artisan make:command UsersCommand
2) Изменяем app\Console\Commands\UsersCommand.php:
1 2 3 4 5 6 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 33 34 35 36 37 38 39 40 41 42 43 44 |
class UsersCommand extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'users'; /** * The console command description. * * @var string */ protected $description = 'list of users'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return mixed */ public function handle() { $headers = ['id', 'name', 'email', 'is_admin']; $users = User::all(['id', 'name', 'email', 'is_admin'])->toArray(); if($users) { $this->table($headers, $users); } else { $this->info('No users'); } } } |
То есть команда называется users. Вызов будет таким: php artisan users . Выводит таблицу с пользователями.
3) Открываем app\Console\Kernel.php и добавляем строку $this->load(__DIR__ . '/Commands'); Иначе команда не будет найдена.
1 2 3 4 5 |
protected function commands() { $this->load(__DIR__ . '/Commands'); require base_path('routes/console.php'); } |