Home > PHP Framework > Laravel > body text

Let's talk about laravel's model time setting

PHPz
Release: 2023-04-11 15:58:31
Original
1451 people have browsed it

Laravel is a popular PHP framework that is currently very popular in the market. It provides various features and tools to make web development easier and more efficient.

This article will discuss a very important concept in the Laravel framework - model time setting. In the Laravel framework, models are usually used to interact with the database, which is achieved by defining the fields and class properties of the tables in the database.

In Laravel, we can control the time settings in the model by modifying the attributes of the model class, mainly including the created_at, updated_at and deleted_at fields.

Creation time setting

In the Laravel framework, created_at refers to the creation time of the row of data in the table. It defaults to the current time and can be automatically set when creating the model.

To control the value of the created_at field, you can set the $timestamps attribute to true in the model class, as shown below:

class Example extends Model
{
    protected $timestamps = true;
}
Copy after login
Copy after login

If you want to customize the name of the created_at field, please use the $createdAt attribute :

class Example extends Model
{
    const CREATED_AT = 'created_date';

    protected $createdAt = 'created_date';
}
Copy after login

Modification time setting

In the Laravel framework, updated_at refers to the last update time of the row of data in the table. It is also the current time by default, and is updated each time the data row is updated. Automatically updates when updates are made.

To control the value of the updated_at field, you can also set the $timestamps attribute to true in the model class, as shown below:

class Example extends Model
{
    protected $timestamps = true;
}
Copy after login
Copy after login

If you want to customize the name of the updated_at field, please use $updatedAt Properties:

class Example extends Model
{
    const UPDATED_AT = 'updated_date';

    protected $updatedAt = 'updated_date';
}
Copy after login

Delete time setting

In the Laravel framework, we can use soft delete to delete data in the table instead of permanently deleting it. In the database, we need to add the deleted_at field so that when soft deletion occurs, this field will be added as the current timestamp.

To control the value of the deleted_at field, you can use the SoftDeletes trait (soft deletion feature) in the model class, as shown below:

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Example extends Model
{
    use SoftDeletes;

    protected $dates = ['deleted_at'];
}
Copy after login

When using soft deletion, the deleted rows will no longer appear in the query and can use the withTrashed method to include soft deleted rows. At the same time, use the onlyTrashed method to return results containing only soft deleted rows.

Summary

In the Laravel framework, we can use model time settings to control the creation time, update time and deletion time of data rows in the table. These settings are defined in the model class and can help us better manage and control the data in the database.

The above is the detailed content of Let's talk about laravel's model time setting. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!