作业 class 不接受控制器在 laravel 中发送的参数

job class is not accepting the argument send from controller in laravel

错误:显示 App\Jobs\ReorderDatabase::handle() 缺少参数 1, 我需要从控制器传递变量,我不需要使用模型, 那么我应该如何进行。

我的控制器功能代码在这里

    public function postData(Request $request)
    {

    $updateRecordsArray = Input::get('order');

    $this->dispatch(new ReorderDatabase($updateRecordsArray));

    return Response::json('Okay');
    }

我的工作 RecorderDatabase 工作代码是

<?php namespace App\Jobs;
 use App\Http\Requests\Request;
 use App\Jobs\Job;
 use Illuminate\Queue\SerializesModels;
 use Illuminate\Queue\InteractsWithQueue;
 use Illuminate\Contracts\Queue\ShouldQueue;
 use Illuminate\Support\Facades\DB;
 use Illuminate\Support\Facades\Log;
 use App\Http\Controllers\DragDropController;

 /**
 * Class ReorderDatabase
 * @package App\Jobs
 */
class ReorderDatabase extends Job implements ShouldQueue
{
use InteractsWithQueue, SerializesModels;

/**
 * Create a new job instance.
 *
 * @return void
 */
public function __construct()
{
}

/**
 * Execute the job.
 *
 * @return void
 */
public function handle($updateRecordsArray)
{
    $i = 1;

    foreach ($updateRecordsArray as $recordID) {


        DB::table('venues')->where('id', '=', $recordID)->update(array('priority' => $i));

        $i++;
     }
    }
  }

如@lagbox 所述,您需要将此参数传递给构造函数而不是 handle 方法。

您的工作 class 应该如下所示:

<?php namespace App\Jobs;
 use App\Http\Requests\Request;
 use App\Jobs\Job;
 use Illuminate\Queue\SerializesModels;
 use Illuminate\Queue\InteractsWithQueue;
 use Illuminate\Contracts\Queue\ShouldQueue;
 use Illuminate\Support\Facades\DB;
 use Illuminate\Support\Facades\Log;
 use App\Http\Controllers\DragDropController;

 /**
 * Class ReorderDatabase
 * @package App\Jobs
 */
class ReorderDatabase extends Job implements ShouldQueue
{
use InteractsWithQueue, SerializesModels;

protected $updateRecordsArray;

/**
 * Create a new job instance.
 *
 * @return void
 */
public function __construct($updateRecordsArray)
{
   $this->updateRecordsArray = $updateRecordsArray;
}

/**
 * Execute the job.
 *
 * @return void
 */
public function handle()
{
    $i = 1;

    foreach ($this->updateRecordsArray as $recordID) {


        DB::table('venues')->where('id', '=', $recordID)->update(array('priority' => $i));

        $i++;
     }
    }
  }