Convert Filament JSON to model bools

orders
Nisse Lommerde 1 month ago
parent c0053a8969
commit 3e2c5d5fac

@ -2,6 +2,7 @@
namespace App\Filament\Resources\OrderResource\Pages; namespace App\Filament\Resources\OrderResource\Pages;
use App\Enums\OrderAttributes;
use App\Filament\Resources\OrderResource; use App\Filament\Resources\OrderResource;
use Filament\Actions; use Filament\Actions;
use Filament\Resources\Pages\EditRecord; use Filament\Resources\Pages\EditRecord;
@ -10,6 +11,34 @@ class EditOrder extends EditRecord
{ {
protected static string $resource = OrderResource::class; protected static string $resource = OrderResource::class;
protected function mutateFormDataBeforeFill(array $data): array
{
foreach (OrderAttributes::cases() as $case) {
if ($data[$case->name]) {
$data['order_attributes'][] = $case->value ?? null;
}
}
return $data;
}
protected function mutateFormDataBeforeSave(array $data): array
{
foreach (OrderAttributes::cases() as $case) {
$data[$case->name] = false;
}
$data['order_attributes'] = array_filter($data['order_attributes']);
foreach ($data['order_attributes'] as $attribute) {
$data[OrderAttributes::from($attribute)->name] = true;
}
unset($data['order_attributes']);
return $data;
}
protected function getHeaderActions(): array protected function getHeaderActions(): array
{ {
return [ return [

@ -0,0 +1,34 @@
<?php
namespace App\Filament\Widgets;
use App\Enums\OrderStatus;
use App\Models\Order;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Filament\Widgets\TableWidget as BaseWidget;
class ActiveOrdersTable extends BaseWidget
{
protected static ?int $sort = 2;
public function table(Table $table): Table
{
return $table
->query(
Order::query()
->where('status', '!=', OrderStatus::SHIPPED)
->where('status', '!=', OrderStatus::INVOICED)
)
->columns([
TextColumn::make('customer.company_name'),
TextColumn::make('customer_po')
->color('code')
->weight('bold'),
TextColumn::make('status')
// ->color(OrderStatus::class)
->badge(),
])
->defaultPaginationPageOption(5);
}
}

@ -8,10 +8,8 @@ use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table; use Filament\Tables\Table;
use Filament\Widgets\TableWidget as BaseWidget; use Filament\Widgets\TableWidget as BaseWidget;
class LatestActiveOrders extends BaseWidget class RushOrdersTable extends BaseWidget
{ {
protected static ?int $sort = 2;
public function table(Table $table): Table public function table(Table $table): Table
{ {
return $table return $table

@ -28,12 +28,18 @@ class Order extends Model
'status', 'status',
'due_date', 'due_date',
'notes', 'notes',
'order_attributes', 'rush',
'repeat',
'new_art',
'event',
'digitizing',
'garments',
'supplied_file',
]; ];
protected $casts = [ protected $casts = [
'status' => OrderStatus::class, 'status' => OrderStatus::class,
'order_attributes' => 'array', // 'order_attributes' => 'array',
]; ];
public static function boot(): void public static function boot(): void

4
composer.lock generated

@ -10338,12 +10338,12 @@
], ],
"aliases": [], "aliases": [],
"minimum-stability": "stable", "minimum-stability": "stable",
"stability-flags": [], "stability-flags": {},
"prefer-stable": true, "prefer-stable": true,
"prefer-lowest": false, "prefer-lowest": false,
"platform": { "platform": {
"php": "^8.2" "php": "^8.2"
}, },
"platform-dev": [], "platform-dev": {},
"plugin-api-version": "2.6.0" "plugin-api-version": "2.6.0"
} }

@ -26,7 +26,16 @@ class OrderFactory extends Factory
'due_date' => $due_date, 'due_date' => $due_date,
'status' => $this->faker->randomElement(OrderStatus::cases())->value, 'status' => $this->faker->randomElement(OrderStatus::cases())->value,
'notes' => $this->faker->words(10, true), 'notes' => $this->faker->words(10, true),
'order_attributes' => ['New Art', 'Garments', 'Rush'],
'rush' => $this->faker->boolean(20),
'repeat' => $this->faker->boolean(),
'new_art' => $this->faker->boolean(),
'event' => $this->faker->boolean(),
'digitizing' => $this->faker->boolean(),
'garments' => $this->faker->boolean(),
'supplied_file' => $this->faker->boolean(),
// 'order_attributes' => ['New Art', 'Garments', 'Rush'],
]; ];
} }
} }

@ -19,7 +19,15 @@ return new class extends Migration
$table->date('due_date'); $table->date('due_date');
$table->string('status'); $table->string('status');
$table->longText('notes')->nullable(); $table->longText('notes')->nullable();
$table->json('order_attributes');
$table->boolean('rush')->default(0);
$table->boolean('repeat')->default(0);
$table->boolean('new_art')->default(0);
$table->boolean('event')->default(0);
$table->boolean('digitizing')->default(0);
$table->boolean('garments')->default(0);
$table->boolean('supplied_file')->default(0);
$table->softDeletes(); $table->softDeletes();
$table->timestamps(); $table->timestamps();
}); });

Loading…
Cancel
Save