【问题标题】:Executing Artisan command with arguments使用参数执行 Artisan 命令
【发布时间】:2017-06-01 04:47:30
【问题描述】:

目前我面临以下问题:

我想在我的数据库更新后自动更新我的搜索索引。 我在 AppServiceProvider 的表上注册了一个 saved() 监听器:

\App\Customer::saved(function(\App\Customer $customer) {
    // Update search index here
 });

在闭包中,我尝试调用 Artisan 命令 (scout:import),将 App\\Customer 传递给命令。我试过了

Artisan::queue('scout:import', ['' => 'App\\\Customer']);
// Fails with message: Uninitialized string offset: 0

Artisan::queue('scout:import', ['model' => 'App\\\Customer']);
// Fails: Cannot redeclare class App\Customer

Artisan::queue('scout:import', ['App\\\Customer']);
// Fails: Not enough arguments (missing: "model")

我没有找到将所需参数放在官方文档中的位置的信息。

我确信它很简单(就像 laravel 中的所有东西一样)但我无法完成它......

【问题讨论】:

    标签: laravel laravel-artisan laravel-scout


    【解决方案1】:

    您无需使用工匠调用与 algolia 同步。 参考 algolia 文档:

    Algolia Laravel Doc

    '每次修改模型时,Laravel 都会发出一个事件。 Scout 正在监听该事件,通知您的应用程序对 Algolia 进行 HTTP 调用以更新其索引。 你没有别的事可做,像往常一样使用你的可搜索类'

    【讨论】:

      【解决方案2】:

      正确的格式是:

      Artisan::queue('email:send', [
          'user' => 1, '--queue' => 'default'
      ]);
      

      根据:https://laravel.com/docs/5.3/artisan#programmatically-executing-commands

      我想说你的中间示例可能是最接近的,并且正在使用正确的参数执行命令,但表面下还有其他事情发生。

      编辑

      只是做了一点挖掘,你需要参考控制台命令的签名,这实际上在表面上并不明显。在你的情况下,你需要参考这个控制台命令:

      https://github.com/laravel/scout/blob/2.0/src/Console/ImportCommand.php

      注意签名标有{model}

      所以你的命令看起来像:

      Artisan::queue('scout:import', ['model' => 'App\\\Customer']);
      

      另一个使用控制器make命令的例子,注意这次我们使用签名段{name}

      Artisan::call('make:controller', ['name'=>'FOOBAR']);
      

      同样,这里可能存在一个潜在问题 - 您应该尝试直接从控制台/终端运行导入命令,看看是否遇到同样的问题。

      【讨论】:

        【解决方案3】:

        试试这个:

        \App\Customer::saved(function(\App\Customer $customer, $input) {
            // Update search index here
        });
        
        Artisan::queue('scout:import {input}', ['App\\\Customer']);
        

        【讨论】:

          猜你喜欢
          • 2019-10-26
          • 2015-08-29
          • 1970-01-01
          • 2020-09-04
          • 2015-11-19
          • 2011-08-25
          • 1970-01-01
          • 2018-09-08
          • 2016-11-17
          相关资源
          最近更新 更多