berinjelablues
1 year ago

Why I can't pass Objects to a Job?

Posted 1 year ago by berinjelablues

On my application I need to pass a object to a Job who will be queued but for some unknown reason that Job can't be done.

For my surprise the job is executed when I remove the object parsed trough the 'dispatch' method.

I'm using the command below to run the worker who execute the job on the queue.

    artisan queue:work

This is my controller:

    use Illuminate\Http\Request;
    use App\Http\Controllers\Controller;
    use App\Jobs\JobToDo;
    use App\Models\ObjectToSend;

    class TestController extends Controller{
        public function create(){
                $objectToSend = ObjectToSend::where('id', 1)->first();
    
                JobToDo::dispatch($objectToSend);
        }
    }

This is my Job:

    use App\Models\ObjectToSend;

    use Illuminate\Bus\Queueable;
    use Illuminate\Queue\SerializesModels;
    use Illuminate\Queue\InteractsWithQueue;
    use Illuminate\Contracts\Queue\ShouldQueue;
    use Illuminate\Foundation\Bus\Dispatchable;
    use Illuminate\Support\Facades\DB;


    class JobToDo implements ShouldQueue {

         use Dispatchable,
                 InteractsWithQueue,
                Queueable,
                SerializesModels;


            public function __construct() {
                 //
            }

        public function handle(ObjectToSend $objectToInsert) {

    
                DB::table('test')->insert(
                        ['name' =>  => $objectToInsert->name,  'object_id' => $objectToInsert->id ]
                );
        }
    }

Please sign in or create an account to participate in this conversation.