Laravel 5.4-将数据从控制器传递到Artisan句柄

Laravel 5.4-将数据从控制器传递到Artisan句柄,laravel,Laravel,为了开始工作,我制作了一个名为MySqlRestore的自定义Artisan命令。 它只是使用转储的sql文件恢复数据库 这是我的MySqlRestore代码: <?php namespace App\Console\Commands; use Illuminate\Console\Command; class MySqlRestore extends Command { /** * The name and signature of the console com

为了开始工作,我制作了一个名为
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=$request->sqlfile
将数据从我的控制器输入我的Artisan
句柄
函数。

您的控制器不应执行Artisan命令,尤其是可能长期运行的命令,如执行数据库备份

<?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';

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

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

        $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);
    }
}

替代,考虑派遣一个执行还原的队列作业。然后,您可以将控制权返回给用户,用户就可以继续进行他们需要做的事情,而不是让网页保持打开状态,这样可能会超时并使数据库处于损坏状态

class DatabaseRestoreController扩展控制器
{
公共函数存储(请求$Request)
{
分派(新的RestoreDatabaseJob($request->input('filename'));
返回redirect()->back()->with success('Restoring database');
}
}
职业类本身:

类RestoreDatabaseJob实现ShouldQueue
{
使用interactiswithqueue;
public$filename;
公共函数构造($filename)
{
$this->filename=$filename;
}
公共函数句柄()
{
Artisan::调用('db:restore'[
'filename'=>$this->filename,
]);
//您可以通知用户恢复已完成
//通过通知发送电子邮件、短信等。
}
}
您的控制器不应执行Artisan命令,尤其是可能长时间运行的命令,如执行数据库备份

替代,考虑派遣一个执行还原的队列作业。然后,您可以将控制权返回给用户,用户就可以继续进行他们需要做的事情,而不是让网页保持打开状态,这样可能会超时并使数据库处于损坏状态

class DatabaseRestoreController扩展控制器
{
公共函数存储(请求$Request)
{
分派(新的RestoreDatabaseJob($request->input('filename'));
返回redirect()->back()->with success('Restoring database');
}
}
职业类本身:

类RestoreDatabaseJob实现ShouldQueue
{
使用interactiswithqueue;
public$filename;
公共函数构造($filename)
{
$this->filename=$filename;
}
公共函数句柄()
{
Artisan::调用('db:restore'[
'filename'=>$this->filename,
]);
//您可以通知用户恢复已完成
//通过通知发送电子邮件、短信等。
}
}

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

e、 g

这叫做使用

$this->argument('dataName');
在控制器中

Artisan::call('db:restore',['test'=> $test]);
namespace-App\Console\Commands;
使用照明\控制台\命令;
类MySqlRestore扩展命令
{
/**
*console命令的名称和签名。
*
*@var字符串
*/
受保护的$signature='db:restore{test}';
/**
*控制台命令说明。
*
*@var字符串
*/
受保护的$description='使用.env中的信息还原数据库';
公共$sqlFile;
/**
*创建一个新的命令实例。
*
*@返回无效
*/
公共函数构造()
{
父项::_构造();
}
/**
*执行控制台命令。
*
*@返回混合
*/
公共函数句柄()
{
$sqlfile=$this->argument('test');
$ds=目录\分隔符;
$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);
}
}
这样说吧
Artisan::调用('db:restore',['test'=>$test]);

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

e、 g

这叫做使用

$this->argument('dataName');
在控制器中

Artisan::call('db:restore',['test'=> $test]);
namespace-App\Console\Commands;
使用照明\控制台\命令;
类MySqlRestore扩展命令
{
/**
*console命令的名称和签名。
*
*@var字符串
*/
受保护的$signature='db:restore{test}';
/**
*控制台命令说明。
*
*@var字符串
*/
受保护的$description='使用.env中的信息还原数据库';
公共$sqlFile;
/**
*创建一个新的命令实例。
*
*@返回无效
*/
公共函数构造()
{
父项::_构造();
}
/**
*执行控制台命令。
*
*@返回混合
*/
公共函数句柄()
{
$sqlfile=$this->argument('test');
$ds=目录\分隔符;
$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);
}
}
这样说吧
Artisan::调用('db:restore',['test'=>$test]);

您是否尝试过通过
构造函数
?如果
$sqlfile=$request->sqlfile
来自我的刀片的
,是否适用?
此外,我还没有使用此
构造函数
。您是否尝试过通过
构造函数
?如果
$sqlfile=$request->sqlfile
ca是否适用
Artisan::call('db:restore',['test'=> $test]);
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]);