Several laravel ORM methods to only enable created_at
This article mainly shares with you a summary of several methods of opening only created_at in laravel ORM. It has a good reference value and I hope it will be helpful to everyone. Let’s follow the editor to take a look, I hope it can help everyone.
Method one:
class User extends Model { public $timestamps = false;//关闭自动维护 public static function boot() { parent::boot(); #只添加created_at不添加updated_at static::creating(function ($model) { $model->created_at = $model->freshTimestamp(); //$model->updated_at = $model->freshTimeStamp(); }); } }
此处有坑:使用create方法创建一条记录时返回值的created的值是这样的: “created_at”: { “date”: “2017-09-27 13:47:12.000000”, “timezone_type”: 3, “timezone”: “Asia/Shanghai” }, 并不是想象中的 “created_at”: “2017-09-27 13:49:39”,
Method two:
class User extends Model { const UPDATED_AT = null;//设置update_at为null //const CREATED_AT = null; }
此处有坑:使用destroy删除会报错 Missing argument 2 for Illuminate\Database\Eloquent\Model::setAttribute() 使用delete不影响,wherein也不影响
Method three:
class User extends Model { //重写setUpdatedAt方法 public function setUpdatedAt($value) { // Do nothing. } //public function setCreatedAt($value) //{ // Do nothing. //} }
Method four:
class User extends Model { //重写setUpdatedAt方法 public function setUpdatedAtAttribute($value) { // Do nothing. } //public function setCreatedAtAttribute($value) //{ // Do nothing. //} }
ps :
It can also be set in Migration (I haven’t tried it specifically, I saw it in other articles)
class CreatePostsTable extends Migration { public function up() { Schema::create('posts', function(Blueprint $table) { $table->timestamp('created_at') ->default(DB::raw('CURRENT_TIMESTAMP')); }); }
Related recommendations:
laravel5 Convert created_at to the format you want
The above is the detailed content of Several laravel ORM methods to only enable created_at. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Laravel - Artisan Commands - Laravel 5.7 comes with new way of treating and testing new commands. It includes a new feature of testing artisan commands and the demonstration is mentioned below ?

Laravel - Pagination Customizations - Laravel includes a feature of pagination which helps a user or a developer to include a pagination feature. Laravel paginator is integrated with the query builder and Eloquent ORM. The paginate method automatical

Method for obtaining the return code when Laravel email sending fails. When using Laravel to develop applications, you often encounter situations where you need to send verification codes. And in reality...

Laravel schedule task run unresponsive troubleshooting When using Laravel's schedule task scheduling, many developers will encounter this problem: schedule:run...

The method of handling Laravel's email failure to send verification code is to use Laravel...

How to implement the table function of custom click to add data in dcatadmin (laravel-admin) When using dcat...

The impact of sharing of Redis connections in Laravel framework and select methods When using Laravel framework and Redis, developers may encounter a problem: through configuration...

Laravel - Dump Server - Laravel dump server comes with the version of Laravel 5.7. The previous versions do not include any dump server. Dump server will be a development dependency in laravel/laravel composer file.
