In laravel Is it necessary that the tables have the "created_at" field as the "updated_at" field?

1

Is there any way I can create those two fields from my console and how can I do it?

I must clarify that the database was already created for me, now I have to implement it in this framework

    
asked by Alberto Yovani PéPérez 24.06.2018 в 19:24
source

1 answer

1

The fields created_at and updated_at are optional if you do not want to use for example the function of soft delete that Laravel has to make logical deletion of data, but above all it depends on what you are solving, your own logic of business will indicate whether or not you need those fields

However, if you require that these columns appear in your table, it can be done as follows, taking into account that:

  • Are fields of type DATETIME
  • Respect the nomenclature of the names of the created_at and updated_at columns
  • Based on this example, I suggest you guide yourself

    Original structure of the example table

    MariaDB [demon]> describe amigos;
    +--------------+--------------+------+-----+---------+----------------+
    | Field        | Type         | Null | Key | Default | Extra          |
    +--------------+--------------+------+-----+---------+----------------+
    | id           | bigint(20)   | NO   | PRI | NULL    | auto_increment |
    | numero       | varchar(100) | NO   | UNI | NULL    |                |
    | name         | varchar(100) | NO   | UNI | NULL    |                |
    | username     | varchar(100) | NO   | UNI | NULL    |                |
    | status_amigo | tinyint(1)   | NO   |     | NULL    |                |
    | user_id      | bigint(20)   | NO   | MUL | NULL    |                |
    +--------------+--------------+------+-----+---------+----------------+
    

    With the ALTER statement we are going to add the following column that would be created_at, we will use AFTER to indicate after which column we want it

    MariaDB [demon]> ALTER TABLE amigos ADD created_at DATETIME AFTER user_id;
    

    The following will repeat the steps but now for the column updated_at also with the AFTER statement to indicate where we want it to be

    MariaDB [demon]> ALTER TABLE amigos ADD updated_at DATETIME AFTER created_at;
    

    With the final result, the following

    MariaDB [demo]> describe amigos;
    +--------------+--------------+------+-----+---------+----------------+
    | Field        | Type         | Null | Key | Default | Extra          |
    +--------------+--------------+------+-----+---------+----------------+
    | id           | bigint(20)   | NO   | PRI | NULL    | auto_increment |
    | numero       | varchar(100) | NO   | UNI | NULL    |                |
    | name         | varchar(100) | NO   | UNI | NULL    |                |
    | username     | varchar(100) | NO   | UNI | NULL    |                |
    | status_amigo | tinyint(1)   | NO   |     | NULL    |                |
    | user_id      | bigint(20)   | NO   | MUL | NULL    |                |
    | created_at   | datetime     | YES  |     | NULL    |                |
    | updated_at   | datetime     | YES  |     | NULL    |                |
    +--------------+--------------+------+-----+---------+----------------+
    
        
    answered by 24.06.2018 в 20:04