Remove the toArray(); from this line
$sales = Sales_details::get()->toArray();
And please use camelcase and sigular form for you class names SalesDetail instead of the mix Sales_details
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Guys iam working with a project,
this is my controller excel function
$sales = Sales_details::get()->toArray();
$sales_array[]=array('Date','Time','Customer Id','Customer Type','Customer Name','Customer Location','customer Area','Customer Booth','Emplyee','Rate per litre','No of litre','Total');
foreach($sales as $e)
{
$sales_array[]=array(
' Date'=>$e->date,
' Time'=>$e->time,
' Customer Id'=>$e->customer_id,
' Customer Type'=>$e->customer_type,
' Customer Name'=>$e->customer_name,
' Customer Location'=>$e->customer_location,
' customer Area'=>$e->customer_area,
' Customer Booth'=>$e->customer_booth,
' Emplyee'=>$e->emp,
' Rate per litre'=>$e->rate_per_litre,
' No of litre'=>$e->no_of_litre,
' Total'=>$e->total
);
}
Excel::create('salesdet', function($excel) use ($sales_array) {
$excel->sheet('mySheet', function($sheet) use ($sales_array)
{
$sheet->fromArray($sales_array);
});
})->download($type);
this is my route
Route::get('downloadExcel/{type}', 'Sales_detailsController@downloadExcel')->name('downloadExcel');
i followed the tutorial
https://www.youtube.com/watch?v=LWLN4p7Cn4E
but it shows an error
Trying to get property 'date' of non-object
whats the problem Kindly suggest some ideas please
@lostdreamer_nl thankz for your answers
i got the answers for both null value and total.
for null value
instead of doing
$sheet->fromArray($ex_array);
do like this
$sheet->fromArray($ex_array, null, 'A1', true);
it works..!!
for total your answer worked.. thank you sooo much
$sales = Sales_details::get()->toArray();
$sales_array = array();
$total = 0;
foreach($sales as $e)
{
$sales_array[] = array(
'Date'=>$e->date,
'Time'=>$e->time,
'Customer Id'=>$e->customer_id,
'Customer Type'=>$e->customer_type,
'Customer Name'=>$e->customer_name,
'Customer Location'=>$e->customer_location,
'Customer Area'=>$e->customer_area,
'Customer Booth'=>$e->customer_booth,
'Emplyee'=>$e->emp,
'Rate per litre'=>$e->rate_per_litre,
'No of litre'=>$e->no_of_litre,
'Total'=>$e->total
);
$total += $e->total;
}
$sales_array[] = array(
'Date'=>'',
'Time'=>'',
'Customer Id'=> '',
'Customer Type'=>'',
'Customer Name'=>'',
'Customer Location'=>'',
'Customer Area'=>'',
'Customer Booth'=>'',
'Emplyee'=> '',
'Rate per litre'=>'',
'No of litre'=>'',
'Total'=> $total
);
Excel::create('salesdet', function($excel) use ($sales_array) {
$excel->sheet('mySheet', function($sheet) use ($sales_array)
{
$sheet->fromArray($sales_array);
});
})->download($type);
Please or to participate in this conversation.