Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Hamelraj's avatar

Maatwebsite Excel export data from database

Hey !

I had a issue with passing data to column depend on other value im passing data like this

$sum = Tour::where('receipt_amount','>=',25000)->count();
$data = array('DATE','RECEIPT NO','AMOUNT','STAMP DUTY');
$sheet->fromArray(array($data),null,'A5',false,false);
$i =0;
 foreach($jobs  as $index => $row){
                    $data=array($row->receipt_date,$row->receipt_no,$row->receipt_amount);
                    $sheet->fromArray(array($data),null,'A5',false,false);
$i++;
 $sheet->appendRow($i+7, array(
                    '', '','Total',$sum*25
                ));
 }

How to put the value in a fourth column if $row->receipt_amount is > 25000 then fourth column shoud be show 25.00 Rs. How can do this ?

0 likes
5 replies
bobbybouwmann's avatar

Something like you have should be fine. I would recommend you to build the data array first and then append everything at one. This was you can debug the data output without generating a new excel sheet every time ;)

1 like
bobbybouwmann's avatar

Well like I said, but everything in an array and see what the output is. From there you can see if you get the expected results.

If you still find this difficult use some old paper and pen and write out in blocks what value you expect in each column in excel. Then make sure you match the data with the positions in your array.

1 like
Hamelraj's avatar
Hamelraj
OP
Best Answer
Level 5

@bobbybouwmann bad coding i know but i did

foreach($jobs  as $index => $row){
                    $data=array($row->receipt_date,$row->receipt_no,$row->receipt_amount,"=IF($row->receipt_amount>25000,25,0)");
                    $sheet->fromArray(array($data),null,'A5',false,false);
   }

Please or to participate in this conversation.