Below, the Laravel Tutorial column will share with you 8 tips on how to use Laravel model timestamps. See if you have never used them. If not, come and collect them. I hope it will be helpful to everyone!
By default, the Laravel Eloquent
model default data tables include created_at
and updated_at
Two fields. Of course, we can make a lot of custom configurations and implement many interesting functions. Here are some examples.
If the data table does not have these two fields, when saving the data Model::create($arrayOfValues); ——You will see SQL error
. Laravel
When automatically filling created_at / updated_at
, these two fields cannot be found.
Disable automatic filling of timestamps, just add the previous attribute in Eloquent Model
:
class Role extends Model { public $timestamps = FALSE; // ... 其他的属性和方法 }
What if you are currently using a non-Laravel
type of database, that is, your timestamp column is named differently? Perhaps, they are called create_time and update_time respectively. Congratulations, you can also define it like this in the model:
class Role extends Model { const CREATED_AT = 'create_time'; const UPDATED_AT = 'update_time';
The following content refers to the official Laravel documentation :
By default, the timestamp is automatically formatted as 'Y-m-d H:i:s'
. If you need to customize the timestamp format, you can set the $dateFormat
property in your model. This property determines the format in which the date is stored in the database and when serialized into an array or JSON:
class Flight extends Model { /** * 日期时间的存储格式 * * @var string */ protected $dateFormat = 'U'; }
When in a many-to-many association, the timestamp will not be automatically filled in, such as the intermediate table role_user# of the user table users and the role table roles ##.
In this model you can define the relationship like this:class User extends Model { public function roles() { return $this->belongsToMany(Role::class); } }
$roleID = 1; $user->roles()->attach($roleID);
does not contain timestamp . And Laravel will not try to automatically populate
created_at/updated_at
created_at/updated_at in the migration file , and then add
->withTimestamps();
public function roles() { return $this->belongsToMany(Role::class)->withTimestamps(); }
And
oldest()Perform timestamp sorting
User::orderBy('created_at', 'desc')->get();
User::latest()->get();
latest() uses created_at sorting.
Correspondingly, there is anoldest(), which will be sorted like this created_at ascending
User::oldest()->get();
updated_at, you can do this:
$lastUpdatedUser = User::latest('updated_at')->first();
Eloquent record is modified, the current timestamp will be automatically used to maintain the
updated_at field, which is a great feature.
timestamps and remember this is temporary:
$user = User::find(1); $user->profile_views_count = 123; $user->timestamps = false; $user->save();
updated_at field without changing the other columns.
So, the following writing method is not recommended:$user->update(['updated_at' => now()]);
$user->touch();
updated_at also hopes to update the record of the superior relationship.
For example, if acomment is updated, then you want to update the updated_at of the post table.
$touches attribute in the model:
class Comment extends Model { protected $touches = ['post']; public function post() { return $this->belongsTo('Post'); } }
Class
created_at and updated_at fields are automatically converted to $dates, so you don’t need to convert them to
Carbon instance, that is, the method of
Carbon can be used.
$user->created_at->addDays(3); now()->diffInDays($user->updated_at);
English original address: https://laraveldaily.com/8-tricks-with-laravel-timestamps/Translation address: https://learnku.com/laravel/ t/39353
The above is the detailed content of [Organization and sharing] 8 tips for using Laravel model timestamps. For more information, please follow other related articles on the PHP Chinese website!