Laravel 作业批处理表与用户模型的关系?

问题描述

我正在使用 Laravel Job Batching 功能,并且我有仪表板,我可以在其中显示 Batch 的进度(已处理、失败、待处理作业等)。

每个用户都有自己的仪表板,我想根据登录用户显示批处理的进度,但是我看不到与批处理表 job_batches用户模型有任何关系。

是否有可能以某种方式与这些表建立关系?或任何其他选择?

谢谢

解决方法

只有我在本地执行作业时修改它:

$this->connection->table($this->table)
        ->where('id',$batch->id)->update([
            'user_id' => Auth::user()->id ?? null,]);
,

这是可能的,但还有很多困难要经过。这也可能是关于扩展 Laravel 功能的一般方法的问题。

一些简单的假设是您在创建批次时使用了某种身份验证,因此您可以执行 Auth::user()->id

通过迁移为 user_id 表创建您的 job_batches

    Schema::table('job_batches',function (Blueprint $table) {
        $table->unsignedBigInteger('user_id')->after('name')->nullable();
        $table->foreign('user_id')->references('id')->on('users');
    });

Laravel 使用 BatchRepositoryjob_batches 表中创建批次,扩展它并添加我们的逻辑以将用户插入到行中。我已将自定义存储库添加到 App\Repositories 命名空间。一般使用当前逻辑,在核心Laravel逻辑执行后更新user_id。

<?php

namespace App\Repostories;

use Illuminate\Bus\DatabaseBatchRepository;
use Illuminate\Bus\PendingBatch;
use Illuminate\Support\Facades\Auth;

class BatchRepository extends DatabaseBatchRepository
{
    public function store(PendingBatch $batch)
    {
        $batch = parent::store($batch); // TODO: Change the autogenerated stub

        $this->connection->table($this->table)
            ->where('id',$batch->id)->update([
                'user_id' => Auth::user()->id,]);

        return $batch;
    }
}

要使 Laravel 使用您的新类,您需要扩展 Container 中的当前类。第三个参数是表名,假设您使用的是默认表。这是在提供程序中完成的。要么把它放在现有的提供者中,要么创建一个新的,记得注册。

use Illuminate\Bus\BatchFactory;
use Illuminate\Bus\BatchRepository;
use Illuminate\Database\Connection;
use App\Repostories\BatchRepository as CustomBatchRepository;

...

public function register()
{
    $this->app->extend(BatchRepository::class,function () {
        return new CustomBatchRepository(resolve(BatchFactory::class),resolve(Connection::class),'job_batches');
    });
}

使用以下代码段进行测试,这会将 user_id 添加到表行中。

Bus::batch([new TestJob(),new TestJob()])->dispatch();

关系

BatchRepositories 返回一个不是 Eloquent 模型的 Batch。因此,我建议为关系目的创建您自己的 Eloquent 模型,并在您想要手头的批处理功能时制作逻辑将其转换为 Batchfinished()

首先是您的 Batch.php 的 Eloquent 模型。同时还准备了 toBatch() 功能,将 Eloquent 模型转换为 Batch 类。

namespace App;

use Illuminate\Bus\BatchRepository;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Batch extends Model
{
    use HasFactory;

    protected $table = 'job_batches';

    public function toBatch()
    {
        return resolve(BatchRepository::class)->toBatch($this);
    }
}

在您的 User.php 上创建您的关系方法。

public function batches()
{
    return $this->hasMany(Batch::class);
}

我使用以下代码段测试了关系设置,效果很好。

User::first()->batches->first()->toBatch();

其次想象有多个批次,您将能够轻松获得具有更高阶函数的 Batch 类。或者将它们用作适当的关系。

User::first()->batches->map->toBatch();

注意

小心导入正确的 BatchBatchRepository 类。我添加了导入以确保您包含正确的导入,以及提供程序的以下片段,使您能够实例化我的自定义批处理存储库。

use App\Repostories\BatchRepository as CustomBatchRepository;

$this->app->bind(CustomBatchRepository::class,function () {
    return new CustomBatchRepository(resolve(BatchFactory::class),'job_batches');
});

您可以自行承担风险,在为此问题创建的粗略测试场中看到 my solution。有一个控制器和用户的关系。不确定其他 StackoverFlow 项目是否有剩余。