Save dates in the right format (Orange)

Asked

Viewed 1,367 times

3

Using Laravel 5.8, I’m using dates. I can even show the date in the right format, using Carbon.

Data de Validade {{\Carbon\Carbon::parse($p->dt_validade)->format('d/m/Y') }}

However I can only save by typing with the format YYYY/MM/DD, I wonder how I could save using the standard DD/MM/YYYY. It is possible to do this right on controller?

Error shown when saving to DD/MM/YYYY format

Invalid datetime format: 1292 Incorrect date value: '10/10/2012' for column 'dt_validity'

Excerpt from the controller where you save the input

$produto->dt_validade = $request->input('dt_validade');

View excerpt:

<div class="col-4">
    <label>Data de Validade:</label>
    <input type="text" class="form-group" id="dt_validade" name="dt_validade" >
</div>

In short: where can I declare to save in the correct format? Thank you.

  • Change the input type to date and try showing here what the return of the input, please.

  • dt_validity: "2080-12-31". With the input "date" it saves normally, but I need to use type text.

4 answers

2


To convert to DB format, you can do so:

'date' => Carbon::parse($request->date)->format('Y-m-d'),

For the user can do so:

   $date = Carbon::parse($variavel->date)->format('d-m-Y');
   return view('formulario', compact('date');

For typing in DD/MM/YYYY format Voce will need a javascript function.

compartilhareditarrestaurarsinalizar

  • Could you explain better how I make the first recommendation? How do I write it or (and how) it adapts to the controller? Forgive ignorance.

1

The store method serves to create a record in the database taking the data that comes from a given form that has a path pointing to this controller method.

public function store(Request $request) {

        $data = [
            'date' => Carbon::parse($request->end_date)->format('Y-m-d'), //Pegando o campo date do input que vem do formulario e convertendo ele para formato aceito pelo banco de dados

        ];

         $create = Campaign::create($data);

         if ($create){
            return redirect(route('suaview_aqui', $create->id))->with('success', trans('app.data_criada'));
        }
    }

0

Substitute:

$produto->dt_validade = $request->input('dt_validade');

For:

use Carbon\Carbon;

$produto->dt_validade = Carbon::createFromFormat('d-m-Y', $request->input('dt_validade'));

Carbon provides a method to create dates based on a format, then you convert in the view to display properly (d-m-Y), and after you type and capture by input you "disconverte" to the standard (Y-m-d).

If an error occurs you can force this way:

$produto->dt_validade = Carbon::createFromFormat('d-m-Y', $request->input('dt_validade'))->format('Y-m-d');

-1

It will depend on your bank if you have created a field with format Date he probably expects a Y-m-d for you to save in the graduate d/m/Y you will have to add in a string field, or something like.

  • Read the other answers. Reading suggestion: https://carbon.nesbot.com/docs/

Browser other questions tagged

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