So I am working on a project using laravel and filamentphp 3.x.
I experienced problems when I wanted to change the order record where the price should appear automatically.
I had the Orders table and Products table, and the price value is stored in Products table.
products table:
public function up(): void
{
Schema::create('orders', function (Blueprint $table) {
$table->id();
$table->foreignIdFor(User::class);
$table->foreignIdFor(Products::class);
$table->string('order_code')->unique();
$table->unsignedInteger('quantity');
// $table->decimal('unit_price', '10', 2);
$table->string('status')->default('pending');
$table->timestamps();
});
}
orders table:
public function up(): void
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('slug')->unique();
$table->text('description')->nullable();
$table->string('features');
$table->unsignedBigInteger('price');
$table->dateTime('start_at')->nullable();
$table->boolean('is_visible')->default(true);
$table->boolean('is_featured')->default(false);
$table->longText('image')->nullable();
$table->timestamps();
});
}
Order models:
public function products()
{
return $this->belongsTo(Products::class);
}
OrderResource.php:
Group::make()->schema([
Section::make()->schema([
Select::make('products_id')
->label('Product')
->options(Products::query()->pluck('name', 'id'))
->native(false)
->reactive()
->afterStateUpdated(fn ($state, Set $set) =>
$set('price', Products::find($state)?->price ?? 0)),
TextInput::make('quantity')
->numeric()
->default(1)
->required()
->live()
->dehydrated(),
TextInput::make('price')
->label('Price')
->prefix('IDR')
->disabled()
->dehydrated()
->numeric()
->required(),
Placeholder::make('gross_amount')
->label('Total Price')
->content(function ($get) {
return $get('quantity') * $get('price');
}),
])
])
I tried to make some change to TextInput::make(‘price’) to this:
TextInput::make('price')
->label('Price')
->prefix('IDR')
->disabled()
->dehydrated()
->numeric()
->required()
->default(function ($record) {
return $record->product ? $record->product->price : 0;
}),
but it still not return any price value.
and then I tried to add a price column to the order table but why do I have to add a price column if I already have price in the product table?