How to echo to console in Laravel and Artisan?

41,088

Solution 1

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...');

Solution 2

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.

Solution 3

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.

Solution 4

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;
}

Solution 5

I like the color added by the Dumper (tested on Laravel 5.3). I think looks a bit nicer than using the echo. The issue I have with the echo out is that it's too easy to be missed, the Dumper it adds a bit of green which catches ones eye:

public function up() {
     // Migration runs //
     (new Illuminate\Support\Debug\Dumper)->dump("A bit more colorful text");
}
Share:
41,088
Ryan Smith
Author by

Ryan Smith

Updated on June 17, 2021

Comments

  • Ryan Smith
    Ryan Smith almost 3 years

    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();
        }
    
    }