Format Pt-Br date controller

1

Good afternoon, if anyone can help me in this problem I will be grateful

I have the following form:

<div class="form-group col-md-12 col-sm-12 col-xs-12">        
        <div class="col-md-2 col-sm-2 col-xs-2 form-label">
            {{Form::label('date', 'Data')}}
        </div>
        <div class="col-md-8 col-sm-10 col-xs-10">
            <div class="input-group">
                <span class="input-group-addon" id="interval">Inicial</span>            
            {{Form::text('dateini', null, ['class' => 'form-control'])}}
                <span class="input-group-addon" id="interval">Final</span>            
            {{Form::text('datefim', null, ['class' => 'form-control'])}}
            </div>
        </div>    
    </div>

How do I put the date in the "d / m / Y" format before finishing this check in the controller?

public function Empenhos(Request $request)
    {
        $query = DB::table('empenho as emp')
                ->select('emp.nrEmpenho as a',DB::raw("DATE_FORMAT(emp.date, '%d/%m/%Y') as b"))
                ->orderby('emp.nrEmpenho');

        if ($request->dateini) $query->where('emp.date', '>=', $request->dateini);
        if ($request->datefim) $query->where('emp.date', '>=', $request->dateini)
                ->where('emp.date', '<=', $request->datefim);

        $table = $query->paginate($request->perPage ? $request->perPage : 20);

        $header = ['Numero', 'Data', 'Tipo', 'Credor', 'Ficha', 'Fonte', 'Valor'];

        return view('results.planejamento.empenhos',
                ['perPage' => $request->perPage, 'title' => $this->title,
                    'title2' => $this->title2[6], 'header' => $header, 'table' => $table, 'return' => 'Empenhos']);
    }

Note: Any questions about the code are available to provide you with any necessary information!

    
asked by anonymous 25.04.2016 / 20:04

1 answer

1

I would do it this way, using the class Carbon\Carbon .

   $data_inicio = Carbon\Carbon::createFromFormat(
       'd/m/Y',
       $request->get('dataini')
  );

I would refactor the code against the query to get more organized. You can leave a callback for queries only and then apply the required operations inside it:

public function empenhos(Request $request)
{
    $callback_search = function ($query) use($request)
    {
        if ($request->has('dataini'))
        {
            $data_ini = Carbon\Carbon::createFromFormat(
                'd/m/Y', $request->get('dataini')
            );

            $query->where('data', '>=', $data_ini);
        }
    };


    $meus_dados = DB::table('minha_tabela')
        ->select(/** **/)
        ->orderBy(/** **/)
        ->where($callback_search)
        ->get();
}
    
26.04.2016 / 18:13