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;
use App\Enums\OrderAttributes;
use App\Filament\Resources\OrderResource;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;
@ -10,6 +11,34 @@ class EditOrder extends EditRecord
{
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
{
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\Widgets\TableWidget as BaseWidget;
class LatestActiveOrders extends BaseWidget
class RushOrdersTable extends BaseWidget
{
protected static ?int $sort = 2;
public function table(Table $table): Table
{
return $table

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

4
composer.lock generated

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

@ -18,15 +18,24 @@ class OrderFactory extends Factory
$due_date = $order_date->copy()->addDays(rand(9, 15));
return [
'created_at' => $order_date,
'updated_at' => Carbon::now(),
'customer_po' => $this->faker->randomNumber(6, true),
'order_type' => $this->faker->randomElement(OrderType::cases())->value,
'order_date' => $order_date,
'due_date' => $due_date,
'status' => $this->faker->randomElement(OrderStatus::cases())->value,
'notes' => $this->faker->words(10, true),
'order_attributes' => ['New Art', 'Garments', 'Rush'],
'created_at' => $order_date,
'updated_at' => Carbon::now(),
'customer_po' => $this->faker->randomNumber(6, true),
'order_type' => $this->faker->randomElement(OrderType::cases())->value,
'order_date' => $order_date,
'due_date' => $due_date,
'status' => $this->faker->randomElement(OrderStatus::cases())->value,
'notes' => $this->faker->words(10, true),
'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->string('status');
$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->timestamps();
});

Loading…
Cancel
Save