【发布时间】:2017-09-19 16:15:28
【问题描述】:
为了开始,我创建了一个名为 MySqlRestore. 的自定义 Artisan 命令
它只是使用转储的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);
}
}
现在在这条线上$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');
}
}
现在我不知道如何将 $sqlfile = $request->sqlfile; 这个数据从我的控制器传递到我的 Artisan handle 函数中。
【问题讨论】:
-
你试过通过
constructor吗? -
如果
$sqlfile = $request->sqlfile来自我 Blade 的<input>?是否适用另外,我还没有使用过这个constructor。
标签: laravel