4

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++;
     }
    }
  }
Vikash
  • 219
  • 5
  • 13
  • you aren't passing that array as an argument to dispatch, it is an argument to the constructor of the job you are creating a new instance of. – lagbox Jan 09 '16 at 07:28
  • I am not getting how to do it, can you please explain – Vikash Jan 09 '16 at 07:35

1 Answers1

5

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++;
     }
    }
  }
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
  • when i am running queue:listen , its showing the error invalid argument supplied for foreach – Vikash Jan 09 '16 at 09:10
  • @Vikash You could then change `$updateRecordsArray = Input::get('order');` into `$updateRecordsArray = (array) Input::get('order');` – Marcin Nabiałek Jan 09 '16 at 09:13
  • Nabialek thanks a lot, been banging my head from last 20 hrs, in lack of basic knowledge, thanks a lot, I appreciate for the help of even basic things. – Vikash Jan 09 '16 at 09:25
  • Hello! I have followed this and getting this error `BindingResolutionException in Container.php line 835: Unresolvable dependency resolving [Parameter #0 [ $nid ]] in class Rogue\Jobs\SendReportbackToPhoenix`. I'm also using the global helper of `dispatch` (dispatch(new ...` instead of `$this->dispatch` and am using Laravel 5.2. Any other thoughts why I would be getting this error? – chloealee Sep 08 '16 at 20:34
  • This only apply to controllers but any ideas about to dispatch jobs with constructor parameters through the RedisDriver? – scruffycoder86 Nov 16 '16 at 09:36