如何使用Laravel 5.8中的种子类在控制台中显示消息? [英] How to display a message in console using a seeder class in Laravel 5.8?

查看:67
本文介绍了如何使用Laravel 5.8中的种子类在控制台中显示消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要在使用Laravel Artisan时显示错误,请 Laravel 5.8官方文档说:

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

$ this-> error('出事了!');

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

但是$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");
    }
}

推荐答案

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

$this的位置是 Seeder 实例, command是此种子控制台 Command 实例,
method()可以是任何可用的命令输出方法.

Where $this is this Seeder instance, command is this seeder console Command instance,
and method() could be any of the command available output methods.

<?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');
    }
}

这篇关于如何使用Laravel 5.8中的种子类在控制台中显示消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆