扩展laravel的SeedCommand

时间:2017-10-27 11:54:55

标签: php laravel-5

尝试对核心db:seed命令进行扩展,以添加一些必要的选项。 扩展Illuminate\Database\Console\Seeds\SeedCommand 并在Kernel.php中注册我的命令,在运行php artisan时向我提供以下输出:

[Illuminate\Contracts\Container\BindingResolutionException]
Target [Illuminate\Database\ConnectionResolverInterface] is not instantiable while building [App\Console\Commands\TenantSeeder].

任何暗示我缺少的东西?下面的课程本身:

<?php

namespace App\Console\Commands;

use Illuminate\Database\Console\Seeds\SeedCommand;
use Illuminate\Console\ConfirmableTrait;
use Symfony\Component\Console\Input\InputOption;
use Illuminate\Database\ConnectionResolverInterface as Resolver;

class TenantSeeder extends SeedCommand
{
    use ConfirmableTrait;
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'tenant:seed';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * The connection resolver instance.
     *
     * @var \Illuminate\Database\ConnectionResolverInterface
     */
    protected $resolver;

    /**
     * Create a new database seed command instance.
     *
     * @param  \Illuminate\Database\ConnectionResolverInterface  $resolver
     * @return void
     */
    public function __construct(Resolver $resolver)
    {
        parent::__construct();

        $this->resolver = $resolver;
    }

    public function getOptions()
    {
        $opts = parent::getOptions();
        return array_merge($opts, [
            ['tenant', null, InputOption::VALUE_REQUIRED, 'Tenant is required to generate tenant-specific data'],
        ]);
    }
}

1 个答案:

答案 0 :(得分:0)

如果您使用像MySQL这样的常规数据库,可以这样做:

String result = "";
        try 
        {
            URL url = new URL(downloadablelink);
            BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
            StringBuilder sb = new StringBuilder();
            String line = br.readLine();
            while (line != null) 
            {
                sb.append(line);
                line = br.readLine();
            }
            result = sb.toString();
        } catch(Exception e) {}