Laravel 5.4 - 将数据从控制器传递给Artisan Handle

Jan*_*ose 1 laravel

为了开始,我做了一个定制的Artisan Command MySqlRestore.

它只是使用转储sql文件来恢复数据库.

这是我的MySqlRestore代码:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class MySqlRestore extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'db:restore';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Restores database using info from .env';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $sqlfile = //data from controller;

        $ds = DIRECTORY_SEPARATOR;
        $host = env('DB_HOST');
        $username = env('DB_USERNAME');
        $password = env('DB_PASSWORD');
        $database = env('DB_DATABASE');

        $mysqlpath = 'C:\xampp\mysql\bin\mysql';
        $path = 'C:\salesandinventory\Backups\\';

        $command = sprintf($mysqlpath . ' --user=' . $username . ' --password=' . $password . ' --host=' . $host . ' ' . $database . ' < ' . $path . $sqlfile);

        exec($command);
    }
}
Run Code Online (Sandbox Code Playgroud)

现在在这一行$sqlfile = //data from controller;,我需要来自我的控制器的数据.

这是我的控制器的样子:

<?php

namespace App\Http\Controllers;

use App\Database;
use Illuminate\Http\Request;
use Artisan;

class DatabaseController extends Controller
{
    public function index()
    {
        return view('database.index');
    }
    public function restoreDatabase(Request $request)
    {
        $sqlfile = $request->sqlfile; // the data I need.

        Artisan::call('db:restore');
        return view('database.index');
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我不知道如何将$sqlfile = $request->sqlfile;这些数据从我的控制器传递到我的Artisan handle功能.

Jer*_*era 6

使用大括号{dataName}通过受保护的$签名传递数据

例如

protected $signature = 'db:restore {dataName}'
Run Code Online (Sandbox Code Playgroud)

它被称为使用

$this->argument('dataName');
Run Code Online (Sandbox Code Playgroud)

在你的控制器中

Artisan::call('db:restore',['test'=> $test]);
Run Code Online (Sandbox Code Playgroud)

namespace App\Console\Commands;

use Illuminate\Console\Command;

class MySqlRestore extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'db:restore {test}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Restores database using info from .env';

    public $sqlFile;
    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $sqlfile = $this->argument('test');

        $ds = DIRECTORY_SEPARATOR;
        $host = env('DB_HOST');
        $username = env('DB_USERNAME');
        $password = env('DB_PASSWORD');
        $database = env('DB_DATABASE');

        $mysqlpath = 'C:\xampp\mysql\bin\mysql';
        $path = 'C:\salesandinventory\Backups\\';

        $command = sprintf($mysqlpath . ' --user=' . $username . ' --password=' . $password . ' --host=' . $host . ' ' . $database . ' < ' . $path . $sqlfile);

        exec($command);
    }
}

Call it like this

Artisan::call('db:restore',['test'=> $test]);
Run Code Online (Sandbox Code Playgroud)