is_pending = 1; // load work order $model->load(['workOrder']); // throw if work order is null if (is_null($model->workOrder)) { throw new CommonException('Work order not found'); } throw_if($model->workOrder->isFailure(), CommonException::class, '工单还没有就绪。'); // change work order status if (auth('sanctum')->check()) { $model->user_id = auth()->id(); $model->workOrder->status = 'user_replied'; } if (auth('module')->check() || auth('admin')->check()) { $model->user_id = null; $model->workOrder->status = 'replied'; broadcast(new UserEvent($model->user_id, 'work-order.replied', $model->workOrder)); } $model->workOrder->save(); }); static::created(function ($model) { if (auth('module')->check()) { $model->workOrder->status = 'replied'; $model->workOrder->save(); } // dispatch dispatch(new \App\Jobs\Module\WorkOrder\Reply($model)); dispatch(new \App\Jobs\Module\WorkOrder\WorkOrder($model->workOrder, 'put')); }); } public function workOrder(): BelongsTo { return $this->belongsTo(WorkOrder::class, 'work_order_id', 'id'); } public function module(): BelongsTo { return $this->belongsTo(Module::class); } public function user(): BelongsTo { return $this->belongsTo(User::class); } // before create public function scopeWorkOrderId($query, $work_order_id) { return $query->where('work_order_id', $work_order_id); } }