Skip to content

Commit 1fee23d

Browse files
scott graysonscott grayson
authored andcommitted
feat: update table columns with defaults and optional fields
- Default columns: name, updated by, updated at - Optional columns: created by, created at, size (toggleable) - Add updated_by field to model and migration - Add updater relationship to LibraryItem model - Add formatFileSize helper method for file size display - Update all creation/update actions to set updated_by field - Size column shows '-' for folders, formatted file size for files
1 parent cb85061 commit 1fee23d

File tree

4 files changed

+54
-6
lines changed

4 files changed

+54
-6
lines changed

src/Models/LibraryItem.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class LibraryItem extends Model implements HasMedia
2424
'type',
2525
'parent_id',
2626
'created_by',
27+
'updated_by',
2728
];
2829

2930
protected $casts = [
@@ -76,6 +77,14 @@ public function creator(): BelongsTo
7677
return $this->belongsTo(\App\Models\User::class, 'created_by');
7778
}
7879

80+
/**
81+
* Get the user who last updated this item.
82+
*/
83+
public function updater(): BelongsTo
84+
{
85+
return $this->belongsTo(\App\Models\User::class, 'updated_by');
86+
}
87+
7988
/**
8089
* Get the permissions for this item.
8190
*/

src/Resources/LibraryItemResource.php

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,24 +92,41 @@ public static function table(Table $table): Table
9292
Tables\Columns\TextColumn::make('name')
9393
->searchable()
9494
->sortable()
95-
->icon(fn (LibraryItem $record): string =>
95+
->icon(fn (LibraryItem $record): string =>
9696
$record->type === 'folder' ? 'heroicon-s-folder' : 'heroicon-o-document'
9797
)
9898
->iconColor('gray')
9999
->iconPosition('before')
100100
->extraAttributes(['class' => 'library-item-name-column']),
101+
Tables\Columns\TextColumn::make('updater.name')
102+
->label('Updated By')
103+
->searchable()
104+
->sortable(),
105+
Tables\Columns\TextColumn::make('updated_at')
106+
->label('Updated At')
107+
->dateTime()
108+
->sortable(),
101109
Tables\Columns\TextColumn::make('creator.name')
102110
->label('Created By')
103111
->searchable()
104-
->sortable(),
112+
->sortable()
113+
->toggleable(),
105114
Tables\Columns\TextColumn::make('created_at')
115+
->label('Created At')
106116
->dateTime()
107117
->sortable()
108-
->toggleable(isToggledHiddenByDefault: true),
109-
Tables\Columns\TextColumn::make('updated_at')
110-
->dateTime()
118+
->toggleable(),
119+
Tables\Columns\TextColumn::make('size')
120+
->label('Size')
121+
->formatStateUsing(function (LibraryItem $record): string {
122+
if ($record->type === 'folder') {
123+
return '-';
124+
}
125+
$media = $record->getFirstMedia('files');
126+
return $media ? static::formatFileSize($media->size) : '-';
127+
})
111128
->sortable()
112-
->toggleable(isToggledHiddenByDefault: true),
129+
->toggleable(),
113130
])
114131
->filters([
115132
Tables\Filters\SelectFilter::make('type')
@@ -151,4 +168,15 @@ public static function getPages(): array
151168
'edit' => Pages\EditLibraryItem::route('/{record}/edit'),
152169
];
153170
}
171+
172+
public static function formatFileSize(int $bytes): string
173+
{
174+
$units = ['B', 'KB', 'MB', 'GB', 'TB'];
175+
176+
for ($i = 0; $bytes > 1024 && $i < count($units) - 1; $i++) {
177+
$bytes /= 1024;
178+
}
179+
180+
return round($bytes, 2) . ' ' . $units[$i];
181+
}
154182
}

src/Resources/Pages/EditLibraryItem.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ protected function getHeaderActions(): array
4343
->action(function (array $data): void {
4444
$this->getRecord()->update([
4545
'parent_id' => $data['parent_id'],
46+
'updated_by' => auth()->user()?->id,
4647
]);
4748

4849
$this->redirect(static::getResource()::getUrl('index', $data['parent_id'] ? ['parent' => $data['parent_id']] : []));
@@ -61,6 +62,14 @@ protected function mutateFormDataBeforeFill(array $data): array
6162
return $data;
6263
}
6364

65+
protected function mutateFormDataBeforeSave(array $data): array
66+
{
67+
// Set the updated_by field
68+
$data['updated_by'] = auth()->user()?->id;
69+
70+
return $data;
71+
}
72+
6473
protected function getForms(): array
6574
{
6675
return [

src/Resources/Pages/ListLibraryItems.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ protected function getHeaderActions(): array
6363
'type' => 'folder',
6464
'parent_id' => $this->parentId,
6565
'created_by' => auth()->user()?->id,
66+
'updated_by' => auth()->user()?->id,
6667
]);
6768

6869
$this->redirect(static::getResource()::getUrl('index', $this->parentId ? ['parent' => $this->parentId] : []));
@@ -93,6 +94,7 @@ protected function getHeaderActions(): array
9394
'type' => 'file',
9495
'parent_id' => $this->parentId,
9596
'created_by' => auth()->user()?->id,
97+
'updated_by' => auth()->user()?->id,
9698
]);
9799

98100
$this->redirect(static::getResource()::getUrl('index', $this->parentId ? ['parent' => $this->parentId] : []));

0 commit comments

Comments
 (0)