New Initial Commit
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\ProductResource\Pages;
|
||||
|
||||
use App\Filament\Resources\ProductResource;
|
||||
use App\Models\ProductImage;
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
|
||||
class CreateProduct extends CreateRecord
|
||||
{
|
||||
protected static string $resource = ProductResource::class;
|
||||
|
||||
protected function mutateFormDataBeforeSave(array $data): array
|
||||
{
|
||||
// Store images in a temporary property
|
||||
if (isset($data['images']) && is_array($data['images'])) {
|
||||
$this->pendingImages = $data['images'];
|
||||
}
|
||||
unset($data['images']);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
protected function afterSave(): void
|
||||
{
|
||||
if (!isset($this->pendingImages)) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($this->pendingImages as $index => $imagePath) {
|
||||
ProductImage::create([
|
||||
'product_id' => $this->record->id,
|
||||
'image_path' => $imagePath,
|
||||
'sort_order' => $index,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\ProductResource\Pages;
|
||||
|
||||
use App\Filament\Resources\ProductResource;
|
||||
use App\Models\ProductImage;
|
||||
use Filament\Actions;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
|
||||
class EditProduct extends EditRecord
|
||||
{
|
||||
protected static string $resource = ProductResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
Actions\DeleteAction::make(),
|
||||
];
|
||||
}
|
||||
|
||||
protected function mutateFormDataBeforeFill(array $data): array
|
||||
{
|
||||
// Load existing images for the form
|
||||
$data['images'] = $this->record->images()->orderBy('sort_order')->pluck('image_path')->toArray();
|
||||
return $data;
|
||||
}
|
||||
|
||||
protected function mutateFormDataBeforeSave(array $data): array
|
||||
{
|
||||
// Store images for processing after save
|
||||
$this->pendingImages = $data['images'] ?? [];
|
||||
unset($data['images']);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
protected function afterSave(): void
|
||||
{
|
||||
if (!isset($this->pendingImages)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$newImages = $this->pendingImages;
|
||||
$existingImages = $this->record->images()->pluck('image_path')->toArray();
|
||||
|
||||
// Find images to delete (in existing but not in new)
|
||||
$imagesToDelete = array_diff($existingImages, $newImages);
|
||||
foreach ($imagesToDelete as $imagePath) {
|
||||
$this->record->images()->where('image_path', $imagePath)->delete();
|
||||
}
|
||||
|
||||
// Add new images and update sort order
|
||||
foreach ($newImages as $index => $imagePath) {
|
||||
ProductImage::firstOrCreate(
|
||||
[
|
||||
'product_id' => $this->record->id,
|
||||
'image_path' => $imagePath,
|
||||
],
|
||||
[
|
||||
'sort_order' => $index,
|
||||
]
|
||||
)->update(['sort_order' => $index]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\ProductResource\Pages;
|
||||
|
||||
use App\Filament\Resources\ProductResource;
|
||||
use Filament\Actions;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
|
||||
class ListProducts extends ListRecords
|
||||
{
|
||||
protected static string $resource = ProductResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
Actions\CreateAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user