›
byrcsc/laravel-approval · 1.x
Create a workflow and approve a proposed model change in four steps.
This walkthrough holds a purchase order amount until a finance user approves it. It assumes Laravel Approval is installed.
Add the Approvable concern and name the workflow the model uses:
use ByRcsc\LaravelApproval\Concerns\Approvable;
use Illuminate\Database\Eloquent\Model;
class PurchaseOrder extends Model
{
use Approvable;
public function approvalWorkflow(): string
{
return 'purchase-order';
}
public function approvableAttributes(): array
{
return ['amount', 'supplier_id', 'notes'];
}
}approvalWorkflow() removes the need to pass the workflow slug during every
submission. approvableAttributes() limits which proposed attributes a request
may hold.
Create one stage and assign the finance user:
use ByRcsc\LaravelApproval\Models\ApprovalWorkflow;
$workflow = ApprovalWorkflow::create([
'name' => 'Purchase orders',
'slug' => 'purchase-order',
]);
$stage = $workflow->stages()->create([
'sequence' => 1,
'name' => 'Finance',
'required_approvals' => 1,
]);
$stage->approvers()->create([
'approver_type' => $financeUser->getMorphClass(),
'approver_id' => $financeUser->getKey(),
]);This stage needs one approval. You can add more stages after the first flow is working.
For workflows stored in source control, see workflow definitions.
Create the purchase order, then submit a higher amount for approval:
$order = PurchaseOrder::create([
'supplier_id' => $supplier->id,
'amount' => 4_000,
]);
$request = $order->submitForApproval(
newAttributes: ['amount' => 10_000],
);The purchase order still contains 4000. The proposed 10000 is stored on the
approval request.
Omit newAttributes when the request should approve the existing record
without changing its attributes.
Record the finance user's decision:
use ByRcsc\LaravelApproval\Facades\Approval;
Approval::approve(
$request,
$financeUser,
'Within the quarterly budget',
);The only stage is now complete. Laravel Approval marks the request as approved and applies the draft:
$request->fresh()->state; // RequestState::Approved
$order->fresh()->amount; // 10000Rejecting or returning a request requires a reason:
Approval::reject($request, $financeUser, 'Over budget');
Approval::returnForRevision(
$request,
$financeUser,
'Attach the supplier quote',
);