首页 > 解决方案 > How to display a message in console using a seeder class in Laravel 5.8?

问题描述

To display an error while using Laravel Artisan the official Laravel 5.8 documentation says:

$this->error('Something went wrong!');

But what's the context of $this?

Following is the contents of the seeder class file:

use Illuminate\Database\Seeder;

class PopulateMyTable extends Seeder
{
    public function run()
    {
        $this->info("Console should show this message");
    }
}

标签: laravellaravel-5

解决方案


您可以通过$this->command->method().

$this这个Seeder实例 在哪里,command这个种子控制台Command实例在哪里,
并且method()可以是任何可用的命令输出方法。

<?php

use Illuminate\Database\Seeder;

use App\User;
use Illuminate\Support\Facades\Hash;

class UserSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $message = 'sample ';
        $this->command->info($message . 'info');
        $this->command->line($message . 'line');
        $this->command->comment($message . 'comment');
        $this->command->question($message . 'question');
        $this->command->error($message . 'error');
        $this->command->warn($message . 'warn');
        $this->command->alert($message . 'alert');
    }
}


推荐阅读