如何为Laravel 9数据库驱动的队列任务记录添加自定义字段?
给Laravel 9 Database驱动队列任务添加自定义字段并展示
1. 扩展jobs表结构
首先需要给默认的jobs表添加自定义字段,用于存储要展示的任务信息。生成迁移文件:
php artisan make:migration add_custom_fields_to_jobs_table
打开生成的迁移文件,添加你需要的字段(示例为任务名称、描述、创建用户ID):
use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { public function up() { Schema::table('jobs', function (Blueprint $table) { $table->string('task_name')->nullable(); $table->text('task_description')->nullable(); $table->unsignedBigInteger('user_id')->nullable(); // 可按需添加更多自定义字段 }); } public function down() { Schema::table('jobs', function (Blueprint $table) { $table->dropColumn(['task_name', 'task_description', 'user_id']); }); } };
运行迁移使字段生效:
php artisan migrate
2. 修改任务类,接收并存储自定义数据
在任务类的构造函数中传入自定义字段值,并重写queue方法,在任务入队时将数据写入jobs表:
namespace App\Jobs; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\Queue; use Illuminate\Queue\SerializesModels; class ProcessExportTask implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; public $taskName; public $taskDescription; public $userId; // 构造函数接收自定义参数 public function __construct(string $taskName, string $taskDescription, int $userId) { $this->taskName = $taskName; $this->taskDescription = $taskDescription; $this->userId = $userId; } // 重写queue方法,任务入队时更新自定义字段 public function queue(Queue $queue, $command) { $jobId = $queue->push($command); // 更新刚创建的jobs记录 $queue->getConnection()->table('jobs') ->where('id', $jobId) ->update([ 'task_name' => $this->taskName, 'task_description' => $this->taskDescription, 'user_id' => $this->userId, ]); return $jobId; } public function handle() { // 你的原有任务执行逻辑 } }
3. 调度任务时传入自定义数据
调用任务时直接传入自定义字段值即可:
// 示例:在控制器中触发任务 ProcessExportTask::dispatch( '用户数据导出', '导出ID为123的用户全部订单数据', auth()->id() );
4. 展示任务列表
在控制器中查询jobs表,获取包含自定义字段的任务数据:
namespace App\Http\Controllers; use Illuminate\Support\Facades\DB; class TaskController extends Controller { public function index() { $tasks = DB::table('jobs') ->select('id', 'task_name', 'task_description', 'user_id', 'created_at', 'reserved_at', 'failed_at') ->orderBy('created_at', 'desc') ->get(); return view('tasks.index', compact('tasks')); } }
在Blade视图中渲染任务列表:
<div class="task-container"> <table class="table"> <thead> <tr> <th>任务名称</th> <th>任务描述</th> <th>创建用户</th> <th>创建时间</th> <th>任务状态</th> </tr> </thead> <tbody> @foreach($tasks as $task) <tr> <td>{{ $task->task_name }}</td> <td>{{ $task->task_description }}</td> <td>{{ $task->user_id }}</td> <td>{{ $task->created_at->format('Y-m-d H:i:s') }}</td> <td> @if($task->failed_at) <span class="text-danger">执行失败</span> @elseif($task->reserved_at) <span class="text-warning">处理中</span> @else <span class="text-success">等待执行</span> @endif </td> </tr> @endforeach </tbody> </table> </div>
额外提示
如果需要展示失败任务,需给failed_jobs表添加对应自定义字段,并在任务类中重写failed方法同步数据:
public function failed(\Throwable $exception) { \Illuminate\Support\Facades\DB::table('failed_jobs') ->where('uuid', $this->job->uuid) ->update([ 'task_name' => $this->taskName, 'task_description' => $this->taskDescription, 'user_id' => $this->userId, ]); }
内容的提问来源于stack exchange,提问作者Mohammad Saleh
相关产品推荐
相关产品推荐

