Php 在Laravel中设定数据库种子时使用进度条
我必须将相当多的数据植入数据库,我希望能够在这种情况下向用户显示进度条。我知道这是有记录的:Php 在Laravel中设定数据库种子时使用进度条,php,symfony,laravel,Php,Symfony,Laravel,我必须将相当多的数据植入数据库,我希望能够在这种情况下向用户显示进度条。我知道这是有记录的: (就在上面) 但我有问题,包括在我的播种机 <?php use Illuminate\Database\Seeder; class SubDivisionRangeSeeder extends Seeder { public function run() { $this->output->createProgressBar(10);
- (就在上面)
<?php
use Illuminate\Database\Seeder;
class SubDivisionRangeSeeder extends Seeder
{
public function run()
{
$this->output->createProgressBar(10);
for ($i = 0; $i < 10; $i++) {
sleep(1);
$this->output->advance();
}
$this->output->finish();
}
}
您的代码无法工作,因为您在错误的类中使用了$this->output
。如果您再次查看您共享的文章,$this output
用于Artisan命令类
事实上,每个Artisan命令都是使用组件生成的
实际上,您正试图在数据库播种器中使用它:)
我的建议是:构建您的种子程序,然后根据需要使用“安装”或“种子”自定义命令调用它们。您可以通过$this->command->getOutput()访问输出。
公共函数运行()
{
$this->command->getOutput()->progressStart(10);
对于($i=0;$i<10;$i++){
睡眠(1);
$this->command->getOutput()->progressAdvance();
}
$this->command->getOutput()->progressFinish();
}
以防万一其他人遇到类似情况并正在寻找解决方案,我创建了以下基类:
<?php
use Illuminate\Database\Schema\Blueprint;
class TestCase extends Illuminate\Foundation\Testing\TestCase
{
protected $progress = 0;
protected function outputInfo($text)
{
$this->output('info', $text, 34);
}
protected function outputSuccess($text)
{
$this->output('success', $text, 32);
}
protected function outputWarning($text)
{
$this->output('warning', $text, 33);
}
protected function outputError($text)
{
$this->output(strtoupper('error'), $text, 31);
}
private function output($status, $text, $colour_code = 34)
{
$this->newLine();
print "\033[1;" . $colour_code . "m" . str_pad($status.':', 8) . " " . $text . " \033[0m";
}
protected function progressDots()
{
if ($this->progress % 20 == 0) {
print '.';
}
}
protected function newLine()
{
echo "\n";
}
/**
* Prevents a warning on the command line
*/
public function testDefault()
{
$this->assertTrue(true);
}
}
这是我的实现代码
<?php
use Illuminate\Database\Seeder;
class MediaTypeTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$media_types = [
['name'=>'movie'],
['name'=>'channel'],
['name'=>'drama'],
// ['name'=>'ebook'],
// ['name'=>'shopping'],
// ['name'=>'reseller'],
// ['name'=>'broadcasting']
];
$this->command->getOutput()->progressStart(count($media_types));
App\Models\Backoffice\MediaType::query()->delete();
foreach($media_types as $media_type){
App\Models\Backoffice\MediaType::create($media_type);
$this->command->getOutput()->progressAdvance();
}
$this->command->getOutput()->progressFinish();
}
}
是的,但是定制的命令无法知道播种机在播种数据循环中的位置以增加进度。当然,你需要创建更复杂的东西,普通的播种机是不够的:)太好了,你甚至不需要自己的artisan命令。把这个放进去就行了。谢谢@Sevenearths这可能是您的答案,因为它完全符合您在示例中的要求,甚至不需要自定义命令。这应该是答案
<?php
use Illuminate\Database\Schema\Blueprint;
class TestCase extends Illuminate\Foundation\Testing\TestCase
{
protected $progress = 0;
protected function outputInfo($text)
{
$this->output('info', $text, 34);
}
protected function outputSuccess($text)
{
$this->output('success', $text, 32);
}
protected function outputWarning($text)
{
$this->output('warning', $text, 33);
}
protected function outputError($text)
{
$this->output(strtoupper('error'), $text, 31);
}
private function output($status, $text, $colour_code = 34)
{
$this->newLine();
print "\033[1;" . $colour_code . "m" . str_pad($status.':', 8) . " " . $text . " \033[0m";
}
protected function progressDots()
{
if ($this->progress % 20 == 0) {
print '.';
}
}
protected function newLine()
{
echo "\n";
}
/**
* Prevents a warning on the command line
*/
public function testDefault()
{
$this->assertTrue(true);
}
}
<?php
class SomethingTests extends TestCase
{
public function testSomething()
{
$array = range(0, 100);
foreach ($array as $value) {
$this->outputInfo('Testing '.$value);
$this->assertGreaterThan(101,$value,'101 is not greater then '.$value);
$this->progressDots();
}
$this->newLine();
}
}
<?php
use Illuminate\Database\Seeder;
class MediaTypeTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$media_types = [
['name'=>'movie'],
['name'=>'channel'],
['name'=>'drama'],
// ['name'=>'ebook'],
// ['name'=>'shopping'],
// ['name'=>'reseller'],
// ['name'=>'broadcasting']
];
$this->command->getOutput()->progressStart(count($media_types));
App\Models\Backoffice\MediaType::query()->delete();
foreach($media_types as $media_type){
App\Models\Backoffice\MediaType::create($media_type);
$this->command->getOutput()->progressAdvance();
}
$this->command->getOutput()->progressFinish();
}
}