Change name of Laravel's created_at and updated_at
The accepted answer may cause problems with updating timestamps unfortunately.
You'd better override consts on your model:
const CREATED_AT = 'post_date';
const UPDATED_AT = 'post_modified';
then methods getCreatedAtColumn
and getUpdatedAtColumn
will return post_date
and post_modified
respectively, but won't do any harm.
For the other columns you need use events like @Oni suggested.
Laravel: Change the timestamps' names in the database
You will need to update your users table migration file in database/migrations
, it will be a file like 2014_10_12_000000_create_users_table.php
.
You likely have $table->timestamps();
in the Schema::create
call.
Looking at the code for timestamp()
in vendor/laravel/framework/src/Illuminate/Database/Schema/Blueprint.php
it reveals:
public function timestamps($precision = 0)
{
$this->timestamp('created_at', $precision)->nullable();
$this->timestamp('updated_at', $precision)->nullable();
}
So:
Schema::create('users', function (Blueprint $table) {
// ..
$table->timestamps();
});
Remove the call to $table->timestamps();
and add add the two columns you want to call the timestamps:
Schema::create('users', function (Blueprint $table) {
// ..
$this->timestamp('user_creation_date', 0)->nullable();
$this->timestamp('user_update_date', 0)->nullable();
});
You will need to run migration again, make sure you back up data as this will drop table and recreate them.
Hope this helps.
Laravel: Switch between created_at and updated_at
Your table should have $table->timestamps();
in your database/migration/bla_bla_table.php
If you have this just open your blade file, find the created_at on your comment section and change it as updated_at.
How to change format of created_at and updated_at in laravel Model?
You can use something like this:
public function getCreatedAtAttribute($date)
{
return Carbon::createFromFormat('Y-m-d H:i:s', $date)->format('Y-m-d H:i');
}
public function getUpdatedAtAttribute($date)
{
return Carbon::createFromFormat('Y-m-d H:i:s', $date)->format('Y-m-d H:i');
}
use these
created_at and updated_at don't update automatically on insert in laravel
insert method is not for eloquent models and doesn't update created_at and updated_at.
you should use create method instead.
create method sets created_at and updated_at automatically
see this answer here
https://stackoverflow.com/a/58075757/7689224
How to change default format at created_at and updated_at value laravel
In your Post
model add two accessor methods like this:
public function getCreatedAtAttribute($date)
{
return Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $date)->format('Y-m-d');
}
public function getUpdatedAtAttribute($date)
{
return Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $date)->format('Y-m-d');
}
Now every time you use these properties from your model to show a date these will be presented differently, just the date without the time, for example:
$post = Post::find(1);
echo $post->created_at; // only Y-m-d formatted date will be displayed
So you don't need to change the original type in the database. to change the type
in your database you need to change it to Date
from Timestamp
and you need to do it from your migration (If your using at all) or directly into your database if you are not using migration. The timestamps()
method adds these fields (using Migration) and to change these fields during the migration you need to remove the timestamps()
method and use date()
instead, for example:
$table->date('created_at');
$table->date('updated_at');
Automatically not insert created_at and updated_at in laravel not working
This is because you haven't set the default value for created_at
and updated_at
. For doing so, you can following 2-3 solutions:
Solution 1
- Go to phpmyadmin
- Select Database table. (In your case, it's
commundityvendordata
) - Then go to structure and set the default value for
created_at
andupdated_at
- And for
updated_at
, you can set the Attribute to on updateCurrent_Timestamp
Solution 2
In your commdata
array, just add the created_at
and updated_at
$commdata = [
...
'created_at' => date("Y-m-d h:i:s"),
'updated_at' => date("Y-m-d h:i:s"),
];
Solution 3
Is to make changes in your Scheme"
Schema::create('commundityvendordata', function (Blueprint $table) {
$table->timestamps('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));
$table->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP'));
});
}
Laravel Eloquent Model created_at to createdAt change is getting reverted
Looks like you are editing the Model
class in vendor folder. When ever you do composer update
its possible your changes will get override, therefore its not a good practice.
You have to add those constants in your model class. For example
use Illuminate\Database\Eloquent\Model;
class Test extends Model {
const CREATED_AT = 'createdAt';
const UPDATED_AT = 'updatedAt';
...
}
Related Topics
How to Create a New Joomla User Account from Within a Script
How to Check If Variable Is Array... or Something Array-Like
Soapfault Exception: Could Not Connect to Host
Load Block Outside Magento, and Apply Current Template
PHP - Listing All Directories and Sub-Directories Recursively in Drop Down Menu
Walk Array Recursively and Print the Path of the Walk
PHP Date Format Yyyy-Mm-Dd Minus or Add One Week from Now
Sort Xml via Attribute Value PHP
How to Send Notification to Android from PHP
What's the Difference Between Is_Null($Var) and ($Var === Null)
How to Implement a Decorator in PHP
Regex/Domdocument - Match and Replace Text Not in a Link
How to Call a Python Script from PHP
Google Calendar API Service Account Error