livewire-powergrid icon indicating copy to clipboard operation
livewire-powergrid copied to clipboard

Incorrect Decimal Value Display in Table for Decimal Fields

Open As3ad10 opened this issue 1 year ago • 0 comments
trafficstars

Have you searched through other issues to see if your problem is already reported or has been fixed?

Yes, I did not find it.

Did you read the documentation?

Yes, I did not find it.

Have you tried to publish the views?

Yes - I didn't work.

Is there an error in the console?

No

PHP Version

8.2

PowerGrid

5.0

Laravel

11.0

Livewire

3.4

Alpine JS

3.4

Theme

Tailwind 3.x

Describe the bug.

When using the livewire-powergrid package, decimal values from the database are not displayed correctly in the table. For example, a value of 10000 is displayed as 10. This issue occurs even when the decimal field is correctly defined in the database and model and when using dd(). but not working in the table.

To Reproduce...

1. Create Migration and Model:

  • Define a migration with a decimal field. For example:
Schema::create('invoices', function (Blueprint $table) {
    $table->id();
    $table->decimal('amount', 15, 2);
    $table->timestamps();
});
  • Create an Eloquent model for this table and cast the amount attribute as a decimal.

    protected $casts = [
        'amount' => 'float',
    ];
    public function amount(): \Illuminate\Database\Eloquent\Casts\Attribute
    {
        return Attribute::make(
            get: fn ($value) => Number::format((float)$value) ,
        );
    }

2. Seed the Database:

  • Insert a record into the table with a significant decimal value, such as 10000.00.

3. Set Up PowerGrid Component:

  • Create a PowerGrid component and define the fields() method like this:
public function fields(): PowerGridFields
{
    return PowerGrid::fields()
        ->add('id')
        ->add('amount');
}

4. Run the Application:

View the table in the browser.

Extra information

Expected Behavior
The amount should display as 10,000

Actual Behavior
The amount displays as 10.

Additional Notes
It appears that the amount field's value is being incorrectly scaled or truncated when displayed in the table. The value retrieved from the database should retain its precision and scale, as defined by the migration and model casting.

As3ad10 avatar Jul 29 '24 20:07 As3ad10