Skip to content

Commit e4a46cb

Browse files
authored
Docs: Enhance moveCard() examples with transaction support
Added examples for custom card movement logic with transaction handling to ensure data integrity.
1 parent a17a12f commit e4a46cb

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

docs/content/2.essentials/1.integration-patterns.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ Override these methods to customize board behavior for your specific needs.
203203

204204
Override `moveCard()` to add custom logic when cards are moved:
205205

206+
#### Simple Updates
207+
For basic updates on the exact same model, you can call the parent method and append your logic:
208+
206209
```php
207210
public function moveCard(
208211
string $cardId,
@@ -222,6 +225,40 @@ public function moveCard(
222225
}
223226
}
224227
```
228+
#### Data Integrity with Transactions (Recommended)
229+
If your custom logic involves complex state machines or updating secondary tables (like activity logs), it is highly recommended to wrap the override in a database transaction. Because parent::moveCard() executes a database update immediately, wrapping the entire method ensures that if your custom logic fails, the database safely rolls back. The frontend's optimistic UI will then gracefully snap the card back to its original column.
230+
231+
```php
232+
use Illuminate\Support\Facades\DB;
233+
234+
public function moveCard(
235+
string $cardId,
236+
string $targetColumnId,
237+
?string $afterCardId = null,
238+
?string $beforeCardId = null
239+
): void {
240+
DB::transaction(function () use ($cardId, $targetColumnId, $afterCardId, $beforeCardId) {
241+
// Let Flowforge handle the stage and position math
242+
parent::moveCard($cardId, $targetColumnId, $afterCardId, $beforeCardId);
243+
244+
// Execute custom atomic logic safely
245+
$task = Task::find($cardId);
246+
247+
if ($targetColumnId === 'completed') {
248+
$task->update(['completed_at' => now()]);
249+
250+
// Safe multi-table insert - rolls back the card move if it fails!
251+
ActivityLog::create([
252+
'task_id' => $task->id,
253+
'action' => 'Task marked as completed'
254+
]);
255+
256+
$task->assignee->notify(new TaskCompletedNotification($task));
257+
}
258+
});
259+
}
260+
```
261+
225262

226263
### Custom Record Fetching
227264

0 commit comments

Comments
 (0)