Skip to content

Commit b48cb31

Browse files
committed
Refactor HasBoardRecords and InteractsWithBoard to implement configurable reordering for improved data retrieval
1 parent fe816c3 commit b48cb31

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

src/Board/Concerns/HasBoardRecords.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,15 @@ public function getBoardRecords(string $columnId): Collection
5353
$limit = $livewire->columnCardLimits[$columnId] ?? 10;
5454
}
5555

56-
return (clone $query)
57-
->where($statusField, $columnId)
58-
->limit($limit)
59-
->get();
56+
$queryClone = (clone $query)->where($statusField, $columnId);
57+
58+
// Apply ordering if configured
59+
$reorderBy = $this->getReorderBy();
60+
if ($reorderBy) {
61+
$queryClone->orderBy($reorderBy['column'], $reorderBy['direction']);
62+
}
63+
64+
return $queryClone->limit($limit)->get();
6065
}
6166

6267
/**

src/Concerns/InteractsWithBoard.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,10 @@ public function updateRecordsOrderAndColumn(string $columnId, array $recordIds):
9797

9898
$updateData = [$statusField => $statusValue];
9999

100-
// Add sort order if the model has that column
101-
if ($this->modelHasSortOrder($record)) {
102-
$updateData['sort_order'] = $index;
100+
// Add sort order if reordering is configured
101+
$reorderBy = $board->getReorderBy();
102+
if ($reorderBy && $this->modelHasOrderColumn($record, $reorderBy['column'])) {
103+
$updateData[$reorderBy['column']] = $index;
103104
}
104105

105106
$success = $record->update($updateData) && $success;
@@ -110,15 +111,15 @@ public function updateRecordsOrderAndColumn(string $columnId, array $recordIds):
110111
}
111112

112113
/**
113-
* Check if model has sort_order column.
114+
* Check if model has the specified order column.
114115
*/
115-
protected function modelHasSortOrder($model): bool
116+
protected function modelHasOrderColumn($model, string $columnName): bool
116117
{
117118
try {
118119
$table = $model->getTable();
119120
$schema = $model->getConnection()->getSchemaBuilder();
120121

121-
return $schema->hasColumn($table, 'sort_order');
122+
return $schema->hasColumn($table, $columnName);
122123
} catch (Exception) {
123124
return false;
124125
}
@@ -194,11 +195,15 @@ public function getBoardColumnRecords(string $columnId): array
194195
$statusField = $board->getColumnIdentifierAttribute() ?? 'status';
195196
$limit = $this->columnCardLimits[$columnId] ?? 10;
196197

197-
return (clone $query)
198-
->where($statusField, $columnId)
199-
->limit($limit)
200-
->get()
201-
->toArray();
198+
$queryClone = (clone $query)->where($statusField, $columnId);
199+
200+
// Apply ordering if configured
201+
$reorderBy = $board->getReorderBy();
202+
if ($reorderBy) {
203+
$queryClone->orderBy($reorderBy['column'], $reorderBy['direction']);
204+
}
205+
206+
return $queryClone->limit($limit)->get()->toArray();
202207
}
203208

204209
/**

0 commit comments

Comments
 (0)