Mathieu Mourareau
Mathieu Mourareau

Reputation: 1220

Query with date results Formatted

I try to display an excel file with the results from a query. everything is going good till the moment I try to display the dates fields with another format with the following code:

>  $licencies->map(function($licencie) {
> 
>                 $licencie['dt_naissance'] = \Carbon\Carbon::createFromFormat('Y-m-d',
> $licencie['dt_naissance'])->format('d/m/y');

I get now an empty excel file . if i remove the code i get the file with the data

How to parse only the "dt_naissance" column from the query?

Here the full code:

public function build()
{
    $licencies = Licencies::where('lb_assurance' , '=' , 'Lafont')
        ->leftJoin('activite_licencie' , 'activite_licencie.id' , '=' , 'licencies.activite_licencie_id')
        ->leftJoin('saisons' , 'saisons.id' , '=' , 'licencies.saison_id')
        ->leftJoin('pays' , 'pays.id' , '=' , 'licencies.pays_naissance_id')
        ->leftJoin('type_licence' , 'type_licence.id' , '=' , 'licencies.type_licence_id')
        ->leftJoin('structures' , 'structures.id' , '=' , 'licencies.structure_id')
        ->leftJoin('civilite' , 'civilite.id' , '=' , 'licencies.civilite_id')
        ->select('civilite.lb_civilite' , 'num_licence' , 'lb_nom' , 'lb_prenom' , 'dt_naissance' , 'pays.fr' ,'activite_licencie.lb_activite'  ,'saisons.lb_saison', 'lb_surclassement' ,  'structures.nom_structure' , 'lb_assurance' , 'cd_dept_naissance' , 'lb_adresse' , 'tel_fix_licencie' , 'tel_port_licencie' , 'adresse_email' , 'licencies.created_at')
        //->whereRaw('DATE(licencies.created_at) = CURRENT_DATE')
        ->get();

         $licencies->map(function($licencie) {

            $licencie['dt_naissance'] = \Carbon\Carbon::createFromFormat('Y-m-d', $licencie['dt_naissance'])->format('d/m/y');

        });

        $excel_file = Excel::create('DailyRecapLicencesLafont', function($excel) use ($licencies) {
        $excel->sheet('Excel', function($sheet) use ($licencies)
        {
            $sheet->fromArray($licencies);
        });
    });

Upvotes: 0

Views: 54

Answers (2)

xar
xar

Reputation: 1439

You forgot to return inside map. It should be

$licencies->map(function($licencie) {

    $licencie['dt_naissance'] = \Carbon\Carbon::createFromFormat('Y-m-d', $licencie['dt_naissance'])->format('d/m/y');

    return $licencie; //this is the part that you miss

});

Upvotes: 1

linktoahref
linktoahref

Reputation: 7972

You could try parsing the date and then formatting it like

$licencie['dt_naissance'] = \Carbon\Carbon::parse($licencie['dt_naissance'])->format('d/m/y');

Upvotes: 1

Related Questions