How is the laravel timestamp used?

0

What happens is that you create a new product form and I want it to be the same as the registration form delivered by laravel, that is, when you insert the product, the created_at and updated_at are automatically filled (already create the columns), then I wanted to know what method does registration, because if I check the controllers I can not find relationships with the timestamp.

    
asked by Matias Muñoz 17.10.2018 в 21:20
source

3 answers

1

Matias Muñoz:

I think if I understood your question well the answer is:

By default, Laravel and specifically Eloquent expect that the columns created_at and updated_at exist in your table . So when you save an item in your database using Laravel, this field is automatically filled in.

If you do not want the fields to be by default, you can configure them in the same model.

If, for example, you do not want to use a $ timestamps you can configure it in the class of your respective model:

public $timestamps = false;

Yes, on the other hand you need to customize your fields, as you have them in your migrations you can do it like this:

//Ejemplos:
const CREATED_AT = 'creation_date';
const UPDATED_AT = 'last_update';

Yes, you want an answer with a concrete example you could upload your migration and that of your models and controllers, to give you an example of how Laravel behaves. I hope I've been useful. Greetings

You can also check the Laravel documentation directly on your website , where all the Documentation on this.

    
answered by 18.10.2018 / 03:30
source
1

What you have to do is a Model , and already with it you do something like this:

$model = new Model();
$model->variable1 = $var1;
$model->variable2 = $var2;
$model->save();

And with the save() the timestamps is saved.

    
answered by 17.10.2018 в 23:17
0

In the Laravel documentation comes the "Create" method, which takes as an object an object with the keys related to your fields

$flight = App\Flight::create(['name' => 'Flight 10']);

In which the dates of "created_at" and "updated_at" will be automatically assigned.

There is also the "insert ()" method that revises the same parameter but unlike this it will not generate automatic dates

    
answered by 17.10.2018 в 23:46