Data formatting

Asked

Viewed 760 times

0

Good morning, could someone help me with this formatting? Well, 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-3 col-sm-10 col-xs-10">            
        {{Form::date('date', null, ['class' => 'form-control'])}}
    </div>     
</div>

I have a query with a select picking the date field:

$query = DB::table('empenho as emp')
            ->select('emp.date as a')->orderby('emp.nrEmpenho');

Then I validate my form information:

if ($request->date) $query->where('emp.date', $request->date);

Everything works perfectly, but it returns me the field in this format: inserir a descrição da imagem aqui

I would like to return the field to me in Day/Month/Year, I thought that using Carbon:parse in the model would solve, but did not solve, I tried to do so in my model Commitment:

<?php

namespace Portal\Entity\Contabilidade;

use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;

class Empenho extends Model
{
    protected $table = 'empenho';
    public $timestamps = false;
    public static $snakeAttributes = false;
    protected $dates = ['date'];

public function getdateAttribute($value)
    {
        return Carbon::parse($value)->format('d/m/Y');
    }
}

It doesn’t work and I don’t know what to do, could someone please help me? Anything that is not clear in the code let me know that I post any other necessary information, thank you !

  • Only $value->format('d/m/Y'); doesn’t work?

  • In fact it only works for a select in a form, when you click on the box it shows the selection formatted, but in the result the format returns to the form of the database. :/

  • If you change the input type to text the result is the same?

  • 1

    But you mixed things up, it won’t work! The definition is in the model, but the select you are using DB::table('tabela'). As far as I know, that’s not right.

  • Wallace, I use Carbon and format the field in the same model ! Even the form everything works perfectly, already in the filter goes back to normal format.

  • Which version of Laravel?

  • 5.1 ---- The friend downstairs helped me, thanks for the answers !

  • Helped but not the best way

  • True, it is seen with "gambiarra", right, informed me, more at least it worked, if you know some way well seen could inform me?

Show 4 more comments

1 answer

2


Try to use the DATE_FORMAT(campo, 'formato') mysql, and use the static method DB::raw() (raw sql), to allow formatting:

$query = DB::table('empenho as emp')
            ->select(DB::raw('DATE_FORMAT(emp.date, "%Y-%m-%d") as a'))->orderby('emp.nrEmpenho');
  • Dude, I found it impossible this didn’t work, but it didn’t work, it gave error, it says that the column is unknown in the list of fields, I formatted the code to not get too big the post, I wonder if it is because my select is searching for other information outside the date field, which should be unlikely too, I’m learning now, please help me.

  • 1

    Try it again, I edited the answer.

  • 1

    You’re the guy, thank you very much, I’m new in the forum and I can’t give you credit, even so stay here my most sincere thanks, thank you very much, God bless you ! <-Problem fully solved-->

  • If it fit, just click on the little arrow below the number, thank you.

Browser other questions tagged

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