Format En date controller

Asked

Viewed 607 times

1

Good afternoon, if anyone can help me with this problem I’d 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 I am available to provide any and all necessary information !

1 answer

1


I would do as follows, using the class Carbon\Carbon.

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

I would refactor the code in relation to the query to be more organized. You can leave a callback only for queries and then apply the necessary operations within 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();
}
  • Thanks for the help, problem solved !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.