Ad

Job Class Is Not Accepting The Argument Send From Controller In Laravel

- 1 answer

Error: Missing argument 1 for App\Jobs\ReorderDatabase::handle() is showing, I need to pass the variable from controller and i need not to use the model, so How I should proceed.

My controller function code is here

    public function postData(Request $request)
    {

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

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

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

My job RecorderDatabase job code is

<?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++;
     }
    }
  }
Ad

Answer

As @lagbox mentioned, you need to pass this argument into constructor and not handle method.

Your job class should look like this:

<?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++;
     }
    }
  }
Ad
source: stackoverflow.com
Ad