php - Saving a Newly Created Eloquent Model as a Job Property Prevents the Job from Being Handled

Solution:

When you dispatch an object to the queue, the object will be saved into the queue, but if you’re passing an Eloquent Model, it will be referenced from the Database by its id (or primary key) instead of being saved entirely into the payload

To avoid that using an unsavec model, just send it as an array.

Controller

$category = new Category;
SaveModelJob::dispatch($category->toArray()); //or build the array from scratch

SaveModelJob

public function __construct($model)
{
    //
    dump('saving as property');
    $this->model = Category::make($model);
    dump('saved as property');
}

If you use the same script for all your models, add another attribute for the class

Controller

$category = new Category;
SaveModelJob::dispatch($category->toArray(), Category::class); //or build the array from scratch

SaveModelJob

public function __construct($model, $eloquentClass)
{
    //
    dump('saving as property');
    $this->model = $eloquentClass::make($model);
    dump('saved as property');
}

Answer

Solution:

I don't see you have saved your model. Please create or save your model, then send it to job:

$category->save();

Also to make sure correct model is passed to job, it's better to inject model:

    public function __construct(Category $category)
    {
        $this->model= $category;
    }

But it still works if you don't inject model class.

Answer

Solution:

I found a trick, we can pass objects as array of objects and it will be saved in payload fully)

Source