'Laravel export Maatwebsite/Excel, restructuring the output

Could somebody tell me how to reformat the excel output, so that i get two excel columns. Right now it gives me two arrays in A1 and B1. like two fields with [1,24,5,3.3] and [3,4,5,6], but I want two columns with the numbers.

Route

Route::get('/exporttable/{id}', [TableExportController::class, 'export']);

TableExportController

use Illuminate\Http\Request;
use App\Exports\TablesExport;
use Maatwebsite\Excel\Facades\Excel;

class TableExportController extends Controller
{
    public function export(Request $request){
        return Excel::download(new TablesExport($request->id), 'tables.xlsx');
    }
}

TableExport.php

use App\Models\Tabula;
use Maatwebsite\Excel\Concerns\FromCollection;

class TablesExport implements FromCollection
{

    /**
    * @return \Illuminate\Support\Collection
    */
    function __construct($id) {
        $this->id = $id;
    }

    public function collection()
    {
        $tabula = Tabula::select('soll',  'haben')->where('id', '=', $this->id)->get();

        return $tabula;
    }
}


Solution 1:[1]

It works with the code of Natvarsinh Parmar - bapu , if you edit the map-function like this

  public function map($tabula): array
    {   
        //unformatieren
        $soll =  $tabula->soll;
        //vom string Randwerte abschneiden: [ und ]
        $soll = substr($soll, 1, -1);
        // in Array umwandeln
        $soll = explode( ',', $soll);
        //das gleiche mit soll
        $haben =  $tabula->haben;
        $haben = substr($haben, 1, -1);
        $haben = explode( ',', $haben);
        // als ersten Wert den Header soll/haben einfügen ins Array
        array_unshift($haben, "haben");
        array_unshift($soll, "soll");

        return [
            $soll, 
            $haben
        ];
    }

So now it works, thanks! Each array covers a row in excel now.

Solution 2:[2]

Make changes as per below in TableExport.php and check

<?php

namespace App\Exports;

use App\Models\Tabula;
use Maatwebsite\Excel\Concerns\FromArray;

class TablesExport implements FromArray
{
    protected $soll;
    protected $haben;
    protected $export_data;

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

    public function array(): array
    {
        $results = Tabula::select('id', 'soll', 'haben')
            ->where('id', $this->id)
            ->get();

        $this->export_data = [];

        $results->each(function($result) {
            preg_match_all('!\d+!', $result->soll, $matches_soll);
            preg_match_all('!\d+!', $result->haben, $matches_haben);

            $this->soll = [];
            $this->haben = [];

            if(isset($matches_soll[0]))
            {
                $this->soll = collect($matches_soll[0]);
            }

            if(isset($matches_haben[0]))
            {
                $this->haben = collect($matches_haben[0]);
            }

            if(count($this->soll) > count($this->haben)) {
                foreach($this->soll as $key => $val){
                    $temp = [];

                    $temp = [
                        $val,
                        (isset($this->haben[$key])) ? $this->haben[$key] : ''
                    ];

                    $this->export_data[] = $temp;
                }
            } else {
                foreach($this->haben as $key => $val){
                    $temp = [];

                    $temp = [
                        (isset($this->soll[$key])) ? $this->soll[$key] : '',
                        $val
                    ];

                    $this->export_data[] = $temp;
                }
            }
        });

        return $this->export_data;
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 J C T
Solution 2