In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the use of Laravel model timestamp tips, the article is very detailed, has a certain reference value, interested friends must read!
By default, the Laravel Eloquent model default datasheet has two fields, created_at and updated_at. Of course, we can do a lot of custom configuration and implement a lot of interesting features. The following is an example.
1. Disable timestamp
If the data table does not have these two fields, Model::create ($arrayOfValues);-- you will see the SQL error when saving the data. Laravel cannot find these two fields when automatically populating created_at / updated_at.
To disable automatic timestamp filling, you only need to add the previous attribute to Eloquent Model:
Class Role extends Model {public $timestamps = FALSE; / /... Other properties and methods}
2. Modify the default list of timestamps
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 in the model:
Class Role extends Model {const CREATED_AT = 'create_time'; const UPDATED_AT =' update_time';}
3. Modify the timestamp date / time format
The following content refers to the official website document official Laravel documentation:
By default, the timestamp is automatically formatted as' Y-m-d hipura'. 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 into an array or JSON:
Class Flight extends Model {/ * * date and time storage format * * @ var string * / protected $dateFormat = 'Utility;}
4. Many-to-many: intermediate table with timestamp
When in many-to-many associations, the timestamp is not automatically populated, such as the intermediate table role_user of the user table users and the role table roles.
In this model, you can define relationships as follows:
Class User extends Model {public function roles () {return $this- > belongsToMany (Role::class);}}
Then when you want to add roles to the user, you can use the following:
$roleID = 1 leading username-> roles ()-> attach ($roleID)
By default, this intermediate table does not contain a timestamp. And Laravel does not attempt to automatically populate created_at/updated_at
But if you want to automatically save the timestamp, you need to add created_at/updated_at to the migration file, and then add-> withTimestamps () to the model association.
Public function roles () {return $this- > belongsToMany (Role::class)-> withTimestamps ();}
5. Use latest () and oldest () for timestamp sorting
There are two "shortcut methods" for sorting using timestamps.
Replace it with:
User::orderBy ('created_at',' desc')-> get ()
It's faster to do this:
User::latest ()-> get ()
By default, latest () uses created_at to sort.
Correspondingly, there is an oldest () that will sort the created_at ascending as follows
User::oldest ()-> get ()
Of course, you can also sort using the other fields you specify. For example, if you want to use updated_at, you can do this:
$lastUpdatedUser = User::latest ('updated_at')-> first ()
6. Do not trigger the modification of updated_at
Whenever an Eloquent record is modified, the updated_at field is automatically maintained with the current timestamp, which is a great feature.
But sometimes you don't want to do this, for example, when you increase a value, you don't think it's a "whole row update."
So, you can do everything as above-- just disable timestamps and remember that this is temporary:
$user = User::find (1); $user- > profile_views_count = 123 user-> timestamps = false;$user- > save ()
7. Update only timestamps and associated timestamps
Contrary to the previous example, you may need to update only the updated_at field without changing the other columns.
Therefore, the following way of writing is not recommended:
$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 records of the parent relationship.
For example, if a comment is updated, you want to update the updated_at of the post table as well.
Then, you need to define the $touches attribute in the model:
Class Comment extends Model {protected $touches = ['post']; public function post () {return $this- > belongsTo (' Post');}}
8. The timestamp field is automatically converted into Carbon class.
The last tip, but more like a reminder, because 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, which means you can use the Carbon method.
For example:
$user- > created_at- > addDays (3); now ()-> diffInDays ($user- > updated_at). These are all the contents of this article entitled "what are the tips for using Laravel model timestamps?" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.