By default, the default data table for the Laravel Eloquent model has two fields: creATED_AT and updated_AT. Of course, we can do a lot of customization and implement a lot of interesting features. Here are some examples.
1. Disable timestamp if the table does not have these two fields, save the data Model::create($arrayOfValues); — you will see SQL error. Laravel could not find these two fields when created_AT/updated_AT was automatically populated. To disable automatic timestamp filling, add a single property on the Eloquent Model:
class Role extends Model { public $timestamps = FALSE; / /... Other properties and methods}Copy the code
2. Change the default timestamp list. What if you are using a non-Laravel database and your timestamp column is named differently? Maybe they’re called create_time and update_time, respectively. Congratulations, you can also define this in the model:
class Role extends Model
{
const CREATED_AT = 'create_time';
const UPDATED_AT = 'update_time';
Copy the code
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 attribute in your model. This property determines the format in which the date is stored in the database and when serialized to an array or JSON:
Class Flight extends Model {/** * @var string */ protected $dateFormat = 'U'; }Copy the code
Many-to-many: timestamp Intermediate tables In a many-to-many association, the timestamp is not automatically filled, for example, ROLE_USER in the users and ROLES tables. In this model you can define relationships as follows:
class User extends Model { public function roles() { return $this->belongsToMany(Role::class); }}Copy the code
Then when you want to add a role to a user, you can use:
$roleID = 1;
$user->roles()->attach($roleID);
Copy the code
By default, this intermediate table does not contain timestamps. And Laravel doesn’t try to auto-populate created_AT/updated_AT but if you want to auto-save the timestamp, you need to add created_AT/updated_AT to the migration file, Then add ->withTimestamps() to the model’s association;
public function roles()
{
return $this->belongsToMany(Role::class)->withTimestamps();
}
Copy the code
There are two “shortcuts” to using timestamp sorting. Instead:
User::orderBy('created_at', 'desc')->get();
Copy the code
User::latest()->get(); By default, latest() is sorted by created_at. In simple (), created_at ascending User::oldest()->get(); Of course, you can also sort using the other fields specified. For example, if you wanted to use updated_AT, you could do this:
$lastUpdatedUser = User::latest('updated_at')->first();
Copy the code
The current timestamp is automatically used to maintain the UPDATed_AT field whenever Eloquent records are modified, which is a great feature. But sometimes you don’t want to do that, for example, when you increment a value and think it’s not a “whole row update.” Well, you can do all of this — just disable timestamps and remember that this is temporary:
$user = User::find(1);
$user->profile_views_count = 123;
$user->timestamps = false;
$user->save();
Copy the code
7. As a reverse of the previous example, you may need to update only the UPDATed_AT field and not change the other columns. $user->update([‘updated_at’ => now()]); $user->update([‘updated_at’ => now()]);
You can use a faster method: $user->touch();
On the other hand, sometimes you want to update not only the updated_AT of the current model, but also the parent record. For example, if a comment is updated, you want to update updated_AT for the POST table as well. So, you need to define the $Touches attribute in the model:
class Comment extends Model { protected $touches = ['post']; public function post() { return $this->belongsTo('Post'); }}Copy the code
8. The timestamp field automatically converts the Carbon class last tip, but more like a reminder as you should already know it. By default, the creATED_AT and updated_AT fields are automatically converted to $dates, so you don’t need to convert them to Carbon instances; you can use the Carbon method instead. Such as:
$user->created_at->addDays(3);
now()->diffInDays($user->updated_at);
Copy the code