Search code examples
phplaravelexport-to-excel

How to convert scientific notation to string with full number laravel


I've got a problem, I want to show the full number in excel but it shows the scientific number. already read another problem but still confused. how can I show a row of full numbers? capture

export.php

    public function collection()
        {
            return Staff::select('nik', 'nama_depan', 'nama_belakang', )->get();
        }
    
        public function headings(): array
        {
            return [
                'NIK',
                'NAMA DEPAN',
                'NAMA BELAKANG',
            ];
        }

Solution

  • The issue is that Excel will automatically convert any string looking like a number as a number.

    You can add a single quote ' before exporting to force Excel to recognize it as a string

    public function collection()
    {
        $collection = Staff::select('nik', 'nama_depan', 'nama_belakang', )->get();
        $collection->map(function ($item, $key) {
            $item->nik = "'" . $item->nik;
            return $item;
        });
        return $collection;
    }