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

bhhussain's avatar

Excel Export with Date Format

Hi,

I am using the below code to export into Excel

It is working fine but the date format I want to implement. I mean, want to export only date without the time.

Can anyone please help me?

namespace App\Exports;
use App\Account;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;

use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\AfterSheet;


class AccountExport implements FromCollection,WithHeadings,ShouldAutoSize,WithEvents
{
    /**
    * @return \Illuminate\Support\Collection
    */
    public function collection()
    {
       // return Account::all();
       //$arr['accounts'] = Account::where('th_comp_code', auth()->user()->company)->where('th_pay_status', 0)->orderBy('th_tran_no','desc')->get();
       //return view('admin.unpaidbills.index')->with($arr);   
       return Account::select(['th_tran_no','created_at','th_supp_name','th_bill_dt','th_bill_no','th_bill_amt','th_purpose','th_emp_name'])
       ->where('th_comp_code', auth()->user()->company)->where('th_pay_status', 0)
       ->get();
       
       
    }

    public function headings(): array
      {
        return [
          'Tran No','Tran Date', 'Supplier name', 'Bill Date', 'Bill No', 'Bill Amount','Purpose','Emp Name' ];
       }

       public function registerEvents(): array
    {
        return [
            AfterSheet::class    => function(AfterSheet $event) {
                $cellRange = 'A1:H1'; // All headers
                $event->sheet->getDelegate()->getStyle($cellRange)->getFont()->setSize(12);
            },
        ];
    }
0 likes
8 replies
bhhussain's avatar

@jlrdw Thanks for your reply.. I already tried this method, It is giving error when I add the date format in the below code.

 return Account::select(['th_tran_no','created_at','th_supp_name','th_bill_dt','th_bill_no','th_bill_amt','th_purpose','th_emp_name'])
       ->where('th_comp_code', auth()->user()->company)->where('th_pay_status', 0)
       ->get();
bhhussain's avatar

@jlrdw Thank you very much for your reply.

part of query .....  DATE_FORMAT( `transactions`.`TransactionDate`, '%m/%d/%Y' ) `tdate`,  .....more query

This code is working in phpmyadmin but when I use the below code in laravel it is showing the below error

return Account::select(['th_tran_no',DATE_FORMAT( `th_bill_dt`, '%d/%m/%Y' ),'th_supp_name','created_at','th_bill_no','th_bill_amt','th_purpose','th_emp_name'])
       ->where('th_comp_code', auth()->user()->company)->where('th_pay_status', 0)
       ->get();
ErrorException
date_format() expects parameter 1 to be DateTimeInterface, null given
bhhussain's avatar

@glenuk Thank you very much for your reply.

When I use the format I am getting the below error message

Symfony\Component\Debug\Exception\FatalErrorException
Class App\Exports\AccountExport contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Maatwebsite\Excel\Concerns\WithMapping::map)
clarg18's avatar

@bhhuassain

Looking at the error, I believe you are implementing the WithMapping interface

class AccountExport implements WithMapping

If this is the case, you will need to write the method that the WithMapping interface dictates; such as this example from the docs:

{    
    /**
    * @var Invoice $invoice
    */
    public function map($invoice): array
    {
        return [
            $invoice->invoice_number,
            $invoice->user->name,
            Date::dateTimeToExcel($invoice->created_at),
        ];
    }
}

See: https://docs.laravel-excel.com/3.1/exports/mapping.html

If this does not resolve your issue, please repost your entire class with your error.

pgs_faizan's avatar

@bhhuassain you were getting the error because in the select query if you want to add SQL function then you have to wrap it with DB::raw e.g:

   return Account::select(['th_tran_no',DB::raw( DATE_FORMAT( `th_bill_dt`, '%d/%m/%Y' ) as 									 
   date),'th_supp_name','created_at','th_bill_no','th_bill_amt','th_purpose','th_emp_name'])
   ->where('th_comp_code', auth()->user()->company)->where('th_pay_status', 0)
   ->get();

Please or to participate in this conversation.