How to echo to console in Laravel and Artisan?

2019-03-17 11:10发布

I was curious, I'm using Laravel and Artisan for my migrations. Is there a method to output information to the console? I can't seem to find any information on this. For example:

<?php

class Generate_Sample_Users{

    public function up(){

        //Echo to console here
        echo "Creating sample users...";

        $generator = new Sample_Data();
        $user_count = 30;
        $users = array();


        for($i=0; $i < $user_count; $i++){
            array_push($users, $generator->generate_user($i));
        }

        DB::table('users')->insert($users);
    }

    public function down(){
        DB::table('users')->delete();
    }

}

7条回答
欢心
2楼-- · 2019-03-17 11:20

Since the chosen answer doesn't seem to work since 4.2, I say just keep it simple:

public function up() {
     // Migration runs //
     echo 'Records processed' . PHP_EOL;
}
查看更多
做自己的国王
3楼-- · 2019-03-17 11:23

'Symfony\Component\Console\Output\ConsoleOutput;' works for me on Laravel 5.2

查看更多
迷人小祖宗
4楼-- · 2019-03-17 11:25

Don't know if you are using Laravel 3 or Laravel 4, and if its also possible in Laravel 3, but i found this in the docs.

$this->info('Creating sample users...');

EDIT

If you switch to database seeds you can use this to display a message

$this->command->info('Creating sample users...');
查看更多
劳资没心,怎么记你
5楼-- · 2019-03-17 11:27

For database seeding in Laravel5, you can use

$this->command->getOutput()->writeln("<info>Your message here</info>");

to print the output on command line.

<info> shows the message in green color where as <error> shows in red color which can be used for error messages.

查看更多
一夜七次
6楼-- · 2019-03-17 11:28

Talking about Laravel 5 (you can check the version you have with php artisan --version), the Migration base-class has no printing method.

A simple echo will do the work, However, if you want, you can extend it and add this functionality:

abstract class MyMigration extends Migration
{
    // colors for console echo
    protected const COLOR_RED = 'COLOR_RED';
    protected const COLOR_GREEN = 'COLOR_GREEN';
    protected const COLOR_YELLOW = 'COLOR_YELLOW';

    protected function logMessage($str, String $color = null)
    {
        switch ($color) {
            case self::COLOR_RED:
                $str = "\033[01;31m$str\033[0m";
                break;
            case self::COLOR_GREEN:
                $str = "\033[01;32m$str\033[0m";
                break;
            case self::COLOR_YELLOW:
                $str = "\033[01;33m$str\033[0m";
            break;

            echo $str . PHP_EOL;
        }
    }
}

and then simply call it with your message:

$this->logMessage("Your message", self::COLOR_RED );
查看更多
姐就是有狂的资本
7楼-- · 2019-03-17 11:31

This works for me

use Symfony\Component\Console\Output\ConsoleOutput;

class MigrateData {

    public function up()
    {
        $output = new ConsoleOutput();

        for($i=0; $i<50000; $i++)
        {
             $output->writeln('Converting '.$i.' of 50000');
        }
     }
}

I have a migration which is converting a large table into a more efficient format and use this to get some progress while it works.

查看更多
登录 后发表回答