1

Laravel Excel export relation columns give array and Arabic words is become like \u0633\u0627\u0645\u0633\u0648\u0646\u062c how to solve it ? I tried to use json_encode($subcategory->name, JSON_UNESCAPED_UNICODE) and it is not working also and give same results. Does anyone have some suggestions to solve this?

Export file

class CategoriesExport implements FromQuery, WithMapping, WithHeadings
{
    use Exportable;

    protected $checked;

    public function __construct($checked)
    {
        $this->checked = $checked;
    }

    public function map($category): array
    {
         return [
            $category->id,
            $category->name,
            $category->status == 1 ? trans('applang.active') : trans('applang.suspended'),
            $category->section->name,
            $category->brands->map(function ($brand){ return $brand->name; }),
            $category->subCategories->map(function ($subcategory){ return $subcategory->name; })
        ];
    }

    public function headings(): array
    {
        return [
            '#ID',
            trans('applang.the_category'),
            trans('applang.status'),
            trans('applang.the_section'),
            trans('applang.the_brand'),
            trans('applang.sub_cat'),
        ];
    }

    public function query()
    {
        return Category::with(['section', 'brands', 'subCategories', 'products'])->whereIn('id', $this->checked);
    }
}
Karl Hill
  • 12,937
  • 5
  • 58
  • 95

2 Answers2

0

In your config/excel.php change the use_bom config to true and try with that.

from What's the difference between UTF-8 and UTF-8 without BOM?

The UTF-8 BOM is a sequence of bytes at the start of a text stream (0xEF, 0xBB, 0xBF) that allows the reader to more reliably guess a file as being encoded in UTF-8.

Mohsen Nazari
  • 1,281
  • 1
  • 3
  • 13
0

To address the issue of array values with Arabic text not displaying correctly in Laravel Excel, you can update the default value_binder in the config/excel.php file. You can set it to a custom class, such as AppDefaultValueBinder, as shown below:

'value_binder' => [
    'default' => \App\Helper\AppDefaultValueBinder::class,
],

You can then create the AppDefaultValueBinder class with the following code:

class AppDefaultValueBinder extends \Maatwebsite\Excel\DefaultValueBinder {
    /**
     * @throws \JsonException
     */
    public function bindValue(\PhpOffice\PhpSpreadsheet\Cell\Cell $cell, $value): bool
    {
        if (is_array($value)) {
            $value = \json_encode($value, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE);
        }

        return parent::bindValue($cell, $value);
   }
}

This custom value binder class will handle binding array values with Arabic text correctly by encoding them as JSON with the JSON_UNESCAPED_UNICODE option to preserve the Arabic characters, and then passing them to the parent bindValue method for further processing in Laravel Excel.

Suhail Kawsara
  • 466
  • 3
  • 11