查询日期结果格式化

时间:2017-04-15 12:51:24

标签: laravel

我尝试使用查询结果显示excel文件。一切顺利,直到我尝试使用以下代码以另一种格式显示日期字段:

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

我现在得到一个空的excel文件。如果我删除代码我得到带有数据的文件

如何仅从查询中解析“dt_naissance”列?

这里是完整的代码:

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);
        });
    });

2 个答案:

答案 0 :(得分:1)

您可以尝试解析日期,然后将其格式化为

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

答案 1 :(得分:1)

你忘了回到 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class another { private JPanel contentPane; private MyPanel panel1; private MyPanel2 panel2; private void displayGUI() { JFrame frame = new JFrame("Card Layout Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel contentPane = new JPanel(); contentPane.setLayout(new CardLayout()); panel1 = new MyPanel(contentPane); panel2 = new MyPanel2(); contentPane.add(panel1, "Panel 1"); contentPane.add(panel2, "Panel 2"); frame.setContentPane(contentPane); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); } public static void main(String... args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new another().displayGUI(); } }); } } class MyPanel extends JPanel { private JTextField How; private JLabel jcomp2; private JLabel jcomp3; private JButton jcomp4; private JPanel contentPane; private JPanel myPanel1; public MyPanel(JPanel panel) { contentPane = panel; //construct components How = new JTextField (1); jcomp2 = new JLabel ("Label2"); jcomp3 = new JLabel ("Label3"); jcomp4 = new JButton ("openNewWindow"); myPanel1 = new JPanel(); //adjust size and set layout setPreferredSize (new Dimension (600, 600)); setLayout (new GridBagLayout()); //set component bounds (only needed by Absolute Positioning) /* How.setBounds (245, 50, 60, 25); jcomp2.setBounds (35, 30, 185, 50); jcomp3.setBounds (250, 30, 60, 20); jcomp4.setLocation(0, 0); jcomp4.setSize(315, 25); */ insert(jcomp2, 0, 0, 1, 1); insert(jcomp3, 0, 1, 1, 1); insert(jcomp4, 1, 0, 1, 1); jcomp4.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { CardLayout cardLayout = (CardLayout) contentPane.getLayout(); cardLayout.next(contentPane); } }); //add components //add (How); add (jcomp2); add (jcomp3); add (jcomp4); } public void insert(Component c, int gridX, int gridY, int gridW, int gridH) { GridBagConstraints constraint = new GridBagConstraints(); constraint.gridx = gridX; constraint.gridy = gridY; constraint.gridwidth = gridW; constraint.gridheight = gridH; constraint.anchor = GridBagConstraints.LINE_START; myPanel1.add(c, constraint); } } class MyPanel2 extends JPanel { private JButton jcomp1; private JButton jcomp2; private JButton jcomp3; private JTextField jcomp4; public MyPanel2() { //construct components jcomp1 = new JButton ("test1"); jcomp2 = new JButton ("test2"); jcomp3 = new JButton ("test3"); jcomp4 = new JTextField (5); //adjust size and set layout setPreferredSize (new Dimension (395, 156)); setLayout (null); //set component bounds (only needed by Absolute Positioning) jcomp1.setBounds (20, 45, 100, 25); jcomp2.setBounds (135, 60, 100, 25); jcomp3.setBounds (260, 35, 100, 25); jcomp4.setBounds (105, 115, 100, 25); //add components add (jcomp1); add (jcomp2); add (jcomp3); add (jcomp4); } } 内。它应该是

map