Ad

`Row `1` Must Be Array` In Laravel

- 1 answer

I am trying to import csv file in laravel with help of maatwebsite . I have query which is bringing data from two table both have relation with each other. it is exporting only one table data when I try to fetch data of both tables it gives me an error of Row1must be array

$data = SaleOrder::where('id',$id)->with('customers')->get()->toArray();

    return Excel::create('Packlist Sale Order '.$id, function($excel) use ($data) {
            $excel->sheet('mySheet', function($sheet) use ($data)
            {




            foreach($data as $customer)
            {

              $sheet->fromArray($customer['customers']);

            }

            $sheet->fromArray($data);


            });
    })->download('xlsx');

I want fetch data of both tables in csv file

Ad

Answer

You are using a with('customers') which means $data is a multi dimensional array with customers already in it, and likely breaking $sheet->fromArray($data);

If you remove the with('customers') from your query and do this:

foreach($data as $salesOrder)
{
   $sheet->fromArray($salesOrder->customers()->get()->toArray());
}

This will load it on demand and leave it out of $data.

Ad
source: stackoverflow.com
Ad