How to solve long string(numeric) issue in datagrid?

- What I am trying to do: I am trying to build simcard management dashboard. - What I did: so for this, I created table grid and displayed simcard data such as card number, phone number etc from external api. - My issue: For example, simcard number format is 9999999999999999999, but it's showing like 1.0E+20 I verified simcard number was loaded correctly from external api, but showing wrong on table grid, so I tried to use formatStateUsing function, but still showing wrong. I can't solve this problem. - Code:
return $table
->columns([
// simoverview record id
TextColumn::make('id')
->toggleable(isToggledHiddenByDefault: true),

// client id
TextColumn::make('portalClient')
->label('Client Id')
->toggleable(isToggledHiddenByDefault: true),

// package
TextColumn::make('PackageName')
->label('Pakke')
->alignLeft(),

// imei
TextColumn::make('IMEI')
->label('Simkort')
->copyable()
->copyMessage('Simkort copied')
->copyMessageDuration(1500)
->formatStateUsing(fn ($state) => (string) $state)
->weight('medium')
->alignLeft(),
]);
return $table
->columns([
// simoverview record id
TextColumn::make('id')
->toggleable(isToggledHiddenByDefault: true),

// client id
TextColumn::make('portalClient')
->label('Client Id')
->toggleable(isToggledHiddenByDefault: true),

// package
TextColumn::make('PackageName')
->label('Pakke')
->alignLeft(),

// imei
TextColumn::make('IMEI')
->label('Simkort')
->copyable()
->copyMessage('Simkort copied')
->copyMessageDuration(1500)
->formatStateUsing(fn ($state) => (string) $state)
->weight('medium')
->alignLeft(),
]);
50 Replies
Shang✨
Shang✨OP2mo ago
I hope someone help me please
No description
undercode
undercode2mo ago
PHP: Hypertext Preprocessor
PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.
Shang✨
Shang✨OP2mo ago
I know this function, but I don't want to add any decimals or commas because its simcard number Hi @undercode Do you know how?
undercode
undercode2mo ago
->formatStateUsing(fn ($state) => number_format(floatval($state), 0, '', ''))
->formatStateUsing(fn ($state) => number_format(floatval($state), 0, '', ''))
Shang✨
Shang✨OP2mo ago
Wow I see, I will try now. Okay I tried.
Shang✨
Shang✨OP2mo ago
The correct simcard number from external api is 89450136240822000001 Now, datagrid is showing like this.
No description
Shang✨
Shang✨OP2mo ago
Correct one
No description
Shang✨
Shang✨OP2mo ago
Well, filament table can't handle large int value? bigint value?
undercode
undercode2mo ago
Maybe you have to store it as string, not int
Shang✨
Shang✨OP2mo ago
Of course I am able to get simcard number as string from external api When I log simcard number, I can see 9999999999999999999 for example correctly. But the problem is only when displaying this on datagrid. I don't understand why filament can't handle this and why filament detects this kind of string as numeric. I think, this is a very critical problem and issue in filament library Do I need to report this issue to filament dev team?
undercode
undercode2mo ago
I'm going to replicate your issue...
Shang✨
Shang✨OP2mo ago
Sounds good thank you so much!
undercode
undercode2mo ago
Is your field defined like: $table->string('imei') ?
Shang✨
Shang✨OP2mo ago
Well, I don't use laravel database such as mysql etc.
undercode
undercode2mo ago
ok I see
Shang✨
Shang✨OP2mo ago
I load data from baserow api But hold on In baserow, yes IMEI filed is string field.
Shang✨
Shang✨OP2mo ago
No description
Shang✨
Shang✨OP2mo ago
Not numeric field As I mentioned above, the problem is filament detects long string(not text) as number, so it shows like 1.0E+20 etc I guess.
undercode
undercode2mo ago
It seems more a php precision thing than a filament one, because number displayed is different when using ini_set('precision', 20)
undercode
undercode2mo ago
Maybe GNU Multiple Precision could help you. https://www.php.net/manual/en/book.gmp.php
PHP: Hypertext Preprocessor
PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.
Shang✨
Shang✨OP2mo ago
Great! Thank you Did you replicate my issue?
undercode
undercode2mo ago
Yes, i did. And with ini_set('precision', 20) the IMEI
89450136240822000001
89450136240822000001
is displayed as
89450136240821993472
89450136240821993472
Shang✨
Shang✨OP2mo ago
Yeah, then, it shows wrong Then, not sure how to solve this. This is very big critical problem.
undercode
undercode2mo ago
Could you share how you load your json and how you parse it?
Shang✨
Shang✨OP2mo ago
Sure I will share php file, okay?
undercode
undercode2mo ago
ok
Shang✨
Shang✨OP2mo ago
app/Filament/Resources/SimcardResource.php
Shang✨
Shang✨OP2mo ago
And json response format is...
{
"id": 12952,
"PackageName": "",
"IMEI": "99999999999999999999",
"Phone": "",
"Activated": null,
"API_Response": "",
"portalClientEmail": "[email protected]",
"portalClient": "999999"
}
{
"id": 12952,
"PackageName": "",
"IMEI": "99999999999999999999",
"Phone": "",
"Activated": null,
"API_Response": "",
"portalClientEmail": "[email protected]",
"portalClient": "999999"
}
But in table() function in SimcardResource.php file, IMEI value shows 1.0E+20 public static function table(Table $table) Simcard.php model works with baserow api. Not database.
undercode
undercode2mo ago
Ok, but you are using Sushi, with uses SQLite to store your data. Try to add this line to your Simcard model:
protected $casts = [ 'IMEI' => 'string'];
protected $casts = [ 'IMEI' => 'string'];
Shang✨
Shang✨OP2mo ago
Ok, let me try. 👍 Then, I don't need part? ->formatStateUsing(fn ($state) => number_format(floatval($state), 0, '', ''))
undercode
undercode2mo ago
No
Shang✨
Shang✨OP2mo ago
Ok, it's showing 89450136240821993472 But it should be like . 89450136240822000001 😖 . Not sure why.
Shang✨
Shang✨OP2mo ago
I added copyable to this field, so when I copied, it's like this. 8.9450136240822E+19
No description
undercode
undercode2mo ago
Ok, remove $casts and add this line:
protected $schema = [
'IMEI' => 'string',
];
protected $schema = [
'IMEI' => 'string',
];
Shang✨
Shang✨OP2mo ago
Ok thank you. Same issue. If you want , you can check directly in my computer via anydesk. Let me think how to solve this problem. I think, this is filament table library issue.
undercode
undercode2mo ago
No, it's not. It's Sushi's Do you have a sqlite database in your database folder?
Shang✨
Shang✨OP2mo ago
But as you can see json, I am able to load simcard data correctly.
undercode
undercode2mo ago
Try to remove it
Shang✨
Shang✨OP2mo ago
Let me check
Shang✨
Shang✨OP2mo ago
There is no sqlite file in database folder.
No description
undercode
undercode2mo ago
This is Sushi function to create the table and its fields:
public function createTable(string $tableName, $firstRow)
{
$this->createTableSafely($tableName, function ($table) use ($firstRow) {
// Add the "id" column if it doesn't already exist in the rows.
if ($this->incrementing && ! array_key_exists($this->primaryKey, $firstRow)) {
$table->increments($this->primaryKey);
}

foreach ($firstRow as $column => $value) {
switch (true) {
case is_int($value):
$type = 'integer';
break;
case is_numeric($value):
$type = 'float';
break;
case is_string($value):
$type = 'string';
break;
case is_object($value) && $value instanceof \DateTime:
$type = 'dateTime';
break;
default:
$type = 'string';
}

if ($column === $this->primaryKey && $type == 'integer') {
$table->increments($this->primaryKey);
continue;
}

$schema = $this->getSchema();

$type = $schema[$column] ?? $type;

$table->{$type}($column)->nullable();
}

if ($this->usesTimestamps() && (! in_array('updated_at', array_keys($firstRow)) || ! in_array('created_at', array_keys($firstRow)))) {
$table->timestamps();
}

$this->afterMigrate($table);
});
}
public function createTable(string $tableName, $firstRow)
{
$this->createTableSafely($tableName, function ($table) use ($firstRow) {
// Add the "id" column if it doesn't already exist in the rows.
if ($this->incrementing && ! array_key_exists($this->primaryKey, $firstRow)) {
$table->increments($this->primaryKey);
}

foreach ($firstRow as $column => $value) {
switch (true) {
case is_int($value):
$type = 'integer';
break;
case is_numeric($value):
$type = 'float';
break;
case is_string($value):
$type = 'string';
break;
case is_object($value) && $value instanceof \DateTime:
$type = 'dateTime';
break;
default:
$type = 'string';
}

if ($column === $this->primaryKey && $type == 'integer') {
$table->increments($this->primaryKey);
continue;
}

$schema = $this->getSchema();

$type = $schema[$column] ?? $type;

$table->{$type}($column)->nullable();
}

if ($this->usesTimestamps() && (! in_array('updated_at', array_keys($firstRow)) || ! in_array('created_at', array_keys($firstRow)))) {
$table->timestamps();
}

$this->afterMigrate($table);
});
}
As you can see, the function creates field types based on the contents of the field.
Shang✨
Shang✨OP2mo ago
I installed Sushi package, but I didn't create any sqlite database file manually, or even I don't know where is sqlite database file. Yeah, I see So, when loading data, Sushi tries to insert data first before showing data to table, yes?
undercode
undercode2mo ago
Exactly. And creates a new table if not exsists.
Shang✨
Shang✨OP2mo ago
I checked createTable function of Sushi, so oh, there is is_int, is_numberic cases. So, Sushi detects simcard number as numeric Not as string. Hmmm
undercode
undercode2mo ago
Exactly! You can specify field types in your $schema variable: https://github.com/calebporzio/sushi?tab=readme-ov-file#custom-schema
GitHub
GitHub - calebporzio/sushi: Eloquent's missing "array" driver.
Eloquent's missing "array" driver. Contribute to calebporzio/sushi development by creating an account on GitHub.
Shang✨
Shang✨OP2mo ago
Awesome! I will try. Well, we tried this already, but didn't work. protected $schema = [ 'IMEI' => 'string', ]; But let me think again.
undercode
undercode2mo ago
Try php artisan cache:clear
Shang✨
Shang✨OP2mo ago
Ok Not working. Actually, I tried to clear cache already.
undercode
undercode2mo ago
I can't think of anything else, sorry. Try to make a repo in github and I'll try when I have time
Shang✨
Shang✨OP2mo ago
Yup, I understand. Thank you so much! for your help and ideas. Oh nice! After adding this part into model ,
protected $schema = [
'IMEI' => 'string',
];
protected $schema = [
'IMEI' => 'string',
];
And removed this part,
>formatStateUsing(fn ($state) => number_format(floatval($state), 0, '', ''))
>formatStateUsing(fn ($state) => number_format(floatval($state), 0, '', ''))
I can see correct simcard number. Of course, I ran
php artisan optimize:clear
php artisan optimize:clear
command, so I can see correctly. Thank you for your help! 👍👍👍 So my problem is solved. @undercode Can you please help me on this ? https://discord.com/channels/883083792112300104/1321818842401603636

Did you find this page helpful?