hasRole('Contractor Admin') or similar. Gate::authorize('approve', $document); $validated = $request->validate([ 'status' => 'required|in:Approved,Rejected', 'comments' => 'nullable|string|max:1000', ]); // Find the global approval chain if it exists $chain = \Modules\ApprovalWorkflow\Models\ApprovalChain::where('approvable_type', Document::class) ->where('approvable_id', $document->id) ->whereIn('status', ['pending', 'in_review']) ->first(); if ($chain) { $service = app(\Modules\ApprovalWorkflow\Services\ApprovalService::class); if ($validated['status'] === 'Approved') { $service->approve($chain, $request->user(), $validated['comments']); } else { $service->reject($chain, $request->user(), $validated['comments']); } } else { // Fallback for older documents that didn't go through the ApprovalWorkflow $document->approvals()->create([ 'approved_by' => $request->user()->id, 'status' => $validated['status'], 'comments' => $validated['comments'] ?? null, 'approved_at' => now(), ]); $document->update([ 'status' => $validated['status'], ]); } return back()->with('success', "Document has been {$validated['status']}."); } }