Using timestamp?

0

When a new migration is created with laravel, a timestamps is created by default in all the tables, and it stores a value in the column created_at and updated_at, these are filled automatically when a new record is stored, I need to obtain the value of the date of creation and modification of a record, the problem is that the updated_at column at the beginning takes the same value as the created_up, my question is, how do I generate my own timestamp for the update.

    
asked by JoséOrdoñez 11.12.2017 в 22:31
source

1 answer

2

Good morning,

What I can think of is that you can do one of these three things:

  • At the moment of displaying the data compare if created_at and updated_at are equal, and in that case do not show updated_at (or make a function / method in the model that does this)

  • Make a migration to add a boolean type column edited that by default is false and once the model is edited in addition to saving the changes it is verified if edited is false , in which case it is changed to true . Then, when displaying the data, if edited is true show column udated_at

  • Make a migration to add a column of type datetime last_update that by default is null and once the model is edited (like the previous method) give it the current time value, then use the column to show the time.

  • For methods 2 and 3 the change of edited or last_update you can do it in different ways:

  • Make the change when you update the model, either in the controller's method or wherever you do it.
  • Observe the event updated or updating of the model, either outside or without of the model .
  • I hope this answers your question and that you have explained me well, any questions you tell me and I will try to clarify myself better or find another solution.

        
    answered by 12.12.2017 / 00:59
    source