I'm following the documentation for Laravel to pass an Eloquent model to a Laravel Job. However, despite my best efforts, I can't pass the Eloquent model - it is always "undefined" or another weird error.
This is for a Laravel 5.7 application, currently running on MacOS/Darwin, MySQL 5.5, and PHP 7.2. This job is launched from another job. I have one job which then dispatches many sub-jobs.
// From the Job itself
public function __construct(\App\Course $courseToDB)
{
$this->courseToDB = $courseToDB;
}
public function handle(App\Course $courseToDB)
{
Log::info($this->courseToDB);
}
// From where I am dispatching the job
\App\Jobs\syncCourse::dispatch($courseToDB)->onConnection('database');
I expect that, when I dispatch the job with $courseToDB
(an \App\Course model), the job will be able to pick that model up and, well, do things with it. The problem I have is that it will not pass the model to the job. Trying to use it even in the simplest logging scenario fails.
Update: My Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;
class Course extends Model
{
protected $fillable = ['id', 'name', 'category_id', 'teacher_id', 'moodle_id', 'summary'];
public function teacher() {
return $this->belongsTo('App\Teacher');
}
public function category() {
return $this->belongsTo('App\Category');
}
public function views() {
return $this->morphMany(
\App\view::class,
'viewable'
);
}
// A few other functions cut out (very simple one-liners)
}
Update: My Migration
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCoursesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('courses', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('moodle_id');
$table->integer('teacher_id');
$table->integer('category_id');
$table->longText('summary')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('courses');
}
}
Update: My Job (syncCourse), which even when doing nothing fails
<?php
namespace App\Jobs;
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\Log;
use App\Course;
class syncCourse implements ShouldQueue
{
public $courseToDB;
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Course $courseToDB)
{
//\App\Course $courseToDB
$this->courseToDB = $courseToDB;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
}
}
Update: From Terminal (when running)
[2018-12-23 01:51:28][3891] Processing: App\Jobs\syncCourse
[2018-12-23 01:51:28][3891] Failed: App\Jobs\syncCourse
[2018-12-23 01:51:28][3892] Processing: App\Jobs\syncCourse
[2018-12-23 01:51:28][3892] Failed: App\Jobs\syncCourse
[2018-12-23 01:51:28][3893] Processing: App\Jobs\syncCourse
[2018-12-23 01:51:28][3893] Failed: App\Jobs\syncCourse
[2018-12-23 01:51:28][3894] Processing: App\Jobs\syncCourse
[2018-12-23 01:51:28][3894] Failed: App\Jobs\syncCourse
[2018-12-23 01:51:28][3895] Processing: App\Jobs\syncCourse
[2018-12-23 01:51:28][3895] Failed: App\Jobs\syncCourse
[2018-12-23 01:51:28][3896] Processing: App\Jobs\syncCourse
[2018-12-23 01:51:28][3896] Failed: App\Jobs\syncCourse
[2018-12-23 01:51:28][3897] Processing: App\Jobs\syncCourse
[2018-12-23 01:51:28][3897] Failed: App\Jobs\syncCourse
[2018-12-23 01:51:28][3898] Processing: App\Jobs\syncCourse
[2018-12-23 01:51:28][3898] Failed: App\Jobs\syncCourse
[2018-12-23 01:51:28][3899] Processing: App\Jobs\syncCourse
[2018-12-23 01:51:28][3899] Failed: App\Jobs\syncCourse
[2018-12-23 01:51:28][3900] Processing: App\Jobs\syncCourse
[2018-12-23 01:51:28][3900] Failed: App\Jobs\syncCourse
[2018-12-23 01:51:28][3901] Processing: App\Jobs\syncCourse
[2018-12-23 01:51:28][3901] Failed: App\Jobs\syncCourse
[2018-12-23 01:51:28][3902] Processing: App\Jobs\syncCourse
[2018-12-23 01:51:28][3902] Failed: App\Jobs\syncCourse
[2018-12-23 01:51:28][3903] Processing: App\Jobs\syncCourse
[2018-12-23 01:51:28][3903] Failed: App\Jobs\syncCourse
[2018-12-23 01:51:28][3904] Processing: App\Jobs\syncCourse
[2018-12-23 01:51:28][3904] Failed: App\Jobs\syncCourse
[2018-12-23 01:51:28][3905] Processing: App\Jobs\syncCourse
[2018-12-23 01:51:28][3905] Failed: App\Jobs\syncCourse
[2018-12-23 01:51:28][3906] Processing: App\Jobs\syncCourse
[2018-12-23 01:51:28][3906] Failed: App\Jobs\syncCourse
[2018-12-23 01:51:28][3907] Processing: App\Jobs\syncCourse
[2018-12-23 01:51:28][3907] Failed: App\Jobs\syncCourse
In my app, dispatchSync
dispatches hundreds of syncCourse
and passes an Eloquent model. Passing the Eloquent model is what is broken, because even if I do absolutely nothing with the passed model, it errors. Laravel.log has zero trace of an error.
UPDATE 2: After running Logging in Service Provider
After turning on the logging in the service provider, all of the syncCourse events just went unfinished. Now my queue:work
looks like this:
[2018-12-23 02:10:41][5829] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5830] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5831] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5832] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5833] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5834] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5835] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5836] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5837] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5838] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5839] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5840] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5841] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5842] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5843] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5844] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5845] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5846] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5847] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5848] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5849] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5850] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5851] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5852] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5853] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5854] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5855] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5856] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5857] Processing: App\Jobs\syncCourse
In the logs:
[2018-12-23 02:10:34] local.ERROR: database
[2018-12-23 02:10:34] local.ERROR: Object of class Illuminate\Queue\Jobs\DatabaseJob could not be converted to string {"exception":"[object] (ErrorException(code: 0): Object of class Illuminate\\Queue\\J