【问题标题】:Can't pass Eloquent model to Laravel Job无法将 Eloquent 模型传递给 Laravel Job
【发布时间】:2019-05-22 09:36:50
【问题描述】:

我正在按照 Laravel 的文档将 Eloquent 模型传递给 Laravel 作业。然而,尽管我尽了最大努力,我还是无法通过 Eloquent 模型——它总是“未定义”或其他奇怪的错误。

这是一个 Laravel 5.7 应用程序,目前在 MacOS/Darwin、MySQL 5.5 和 PHP 7.2 上运行。该作业是从另一个作业启动的。我有一份工作,然后分派许多子工作。

// 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');

我希望,当我使用$courseToDB(一个\App\Course 模型)发送作业时,作业将能够选择该模型并使用它进行操作。我遇到的问题是它不会将模型传递给工作。即使在最简单的日志记录场景中尝试使用它也会失败。

更新:我的模型

<?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)

}

更新:我的迁移

<?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');
    }
}

更新:我的工作 (syncCourse),即使什么都不做也会失败

<?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()
    {        

    }
}

更新:从终端(运行时)

[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

在我的应用程序中,dispatchSync 调度数百个syncCourse 并传递一个 Eloquent 模型。传递 Eloquent 模型是有问题的,因为即使我对传递的模型完全什么都不做,它也会出错。 Laravel.log 的错误痕迹为零。

更新 2:在服务提供商中运行登录后

在服务提供者中打开日志记录后,所有的同步课程事件都没有完成。现在我的queue:work 看起来像这样:

[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

在日志中:

[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

【问题讨论】:

  • 您是否收到任何错误或日志消息?如果有,请在问题中添加。
  • 请发布您工作的完整代码,尤其是特征。

标签: php laravel laravel-queue


【解决方案1】:

尝试如下使用,

在类声明上方添加use \App\Course;

在你的方法中使用Course::function_name()调用模型

在你的模型函数声明中必须喜欢public static function function_name()

或者分享你现在遇到的错误。

【讨论】:

    【解决方案2】:

    确保调试出错的地方。这是complete reference 的操作方法。

    1.要为failed_jobs 表创建迁移,您可以使用queue:failed-table 命令:

       php artisan queue:failed-table
    
       php artisan migrate
    
    1. handle() 方法之后包含以下方法。

      public function failed(Exception $exception)
      {
         //Your expection
         dd($exception)
      }
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-14
      • 2018-08-20
      • 2021-12-23
      • 1970-01-01
      • 2020-06-18
      • 1970-01-01
      • 2013-01-05
      相关资源
      最近更新 更多