php 如何在 Laravel 和 Artisan 中回显到控制台?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16733957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to echo to console in Laravel and Artisan?
提问by Ryan Smith
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:
我很好奇,我使用 Laravel 和 Artisan 进行迁移。有没有办法将信息输出到控制台?我似乎无法找到任何有关此的信息。例如:
<?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();
}
}
回答by HymanPoint
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.
不知道您使用的是 Laravel 3 还是 Laravel 4,是否也可以在 Laravel 3 中使用,但我在文档中找到了这个。
$this->info('Creating sample users...');
EDIT
编辑
If you switch to database seedsyou can use this to display a message
如果您切换到数据库种子,您可以使用它来显示一条消息
$this->command->info('Creating sample users...');
回答by mtopley
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.
我有一个迁移,它正在将一个大表转换为更有效的格式,并在它工作时使用它来取得一些进展。
回答by Captain Hypertext
Since the chosen answer doesn't seem to work since 4.2, I say just keep it simple:
由于所选答案自 4.2 起似乎不起作用,我说保持简单:
public function up() {
// Migration runs //
echo 'Records processed' . PHP_EOL;
}
回答by Chittaranjan
For database seeding in Laravel5, you can use
对于 Laravel5 中的数据库播种,您可以使用
$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.
<info>
以绿色显示消息,<error>
以红色显示,可用于错误消息。
回答by gsaqui
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:
我喜欢 Dumper 添加的颜色(在 Laravel 5.3 上测试)。我认为看起来比使用回声好一点。我对回声的问题是它太容易被错过了,它添加了一点绿色,吸引了眼球:
public function up() {
// Migration runs //
(new Illuminate\Support\Debug\Dumper)->dump("A bit more colorful text");
}
回答by xinHAOr
'Symfony\Component\Console\Output\ConsoleOutput;' works for me on Laravel 5.2
'Symfony\Component\Console\Output\ConsoleOutput;' 在 Laravel 5.2 上对我有用
回答by guyaloni
Talking about Laravel 5 (you can check the version you have with php artisan --version
), the Migration base-class has no printing method.
谈到 Laravel 5(您可以查看您拥有的版本php artisan --version
),Migration 基类没有打印方法。
A simple echo
will do the work, However, if you want, you can extend it and add this functionality:
一个简单的echo
将完成工作,但是,如果您愿意,可以扩展它并添加此功能:
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 = "3[01;31m$str3[0m";
break;
case self::COLOR_GREEN:
$str = "3[01;32m$str3[0m";
break;
case self::COLOR_YELLOW:
$str = "3[01;33m$str3[0m";
break;
echo $str . PHP_EOL;
}
}
}
and then simply call it with your message:
然后简单地用你的消息调用它:
$this->logMessage("Your message", self::COLOR_RED );