Home » Php » how to only use created_at in laravel

how to only use created_at in laravel

Posted by: admin November 30, 2017 Leave a comment

Questions:

everyone.

I am new to laravel.

this is my questions:

this can custom timestamps name

const CREATED_AT = 'created';
const UPDATED_AT = 'updated';

this can disable timestamps

public $timestamps = false;

I want only use created_at , how to do it ?

Answers:

Eloquent does not provide such functionality out of the box, but you can create it on your own using the creating event callback:

class User extends Eloquent {

    public $timestamps = false;

    public static function boot()
    {
        parent::boot();

        static::creating(function ($model) {
            $model->created_at = $model->freshTimestamp();
        });
    }

}

Questions:
Answers:

Use on the top:

const UPDATED_AT = null;

And for ‘created_at’ field, you can use:

const CREATED_AT = null;

UPDATE FOR LARAVEL 5.5.0 – 5.5.5

This was broken in Laravel 5.5.0 (and fixed again in 5.5.5).

If you are using 5.5.x, make sure you are on the newest version.

If for some reason you cannot be on the newest version, here is a workaround.

Set the public $timestamps to false:

public $timestamps = false;

And when necessary:

public function setCreatedAtAttribute($value) { 
    $this->attributes['created_at'] = \Carbon\Carbon::now(); 
}

OR

public function setUpdatedAtAttribute($value) { 
    $this->attributes['updated_at'] = \Carbon\Carbon::now(); 
}

When the two fields “created_at” and “updated_at” are required, you do not have to do anything, of course 😉

Questions:
Answers:

My solution:

class CreatePostsTable extends Migration {

   public function up() {

      Schema::create('posts', function(Blueprint $table){

         $table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));
   });
}

This works for me

Questions:
Answers:

I have a better answer from here for Laravel 5.2 or above.

U can use this in model-

class User extends Model
{
    public $timestamps = false; // disable all behaviour
    public $timestamps = true; // enable all behaviour
    public $timestamps = [ "created_at" ]; // enable only to created_at
    public $timestamps = [ "updated_at" ]; // enable only to updated_at
    public $timestamps = [ "created_at", "updated_at" ]; // same that true
}

So, for your question, the answer is –

public $timestamps = [ "created_at" ]; // enable only to created_at

Questions:
Answers:

To disable only updated_at, you can override Model::setUpdatedAt() method, like following :

public function setUpdatedAt($value) {
    // Do nothing.
}

Of course, if you wanted to do this for the created_at column, it’s just as easy. And that’s work for Laravel 5.1

Questions:
Answers:

Approach with only setUpdatedAt did not work with Laravel 5.1.7 because it has one more place where updated_at gets processed.

Model class performUpdate method calls Builder class method:

public function update(array $values)
{
    return $this->query->update($this->addUpdatedAtColumn($values));
}

which leads us to

return Arr::add($values, $column, $this->model->freshTimestampString());

I’m not sure, why Laravel is processing updated_at twice – in performUpdate with $this->updateTimestamps() and then later in Builder with $this->addUpdatedAtColumn($values).

With some debugging, I found that you have to also update your model with getUpdatedAtColumn override. At first I was afraid that it will try to update a non-existent field “null”, but it turned out that Arr::add is smart enough to ignore null keys.

So, in your model class add these:

public function setUpdatedAt($value)
{
    // Do nothing.
}

public function getUpdatedAtColumn()
{
    return null;
}

This should be enough to disable both updates.

Questions:
Answers:

In version 5.3 I just did public $timestamps = false; and then added protected $fillable = [‘created_at’]. Note: You can add anything you want, just make sure it matches the one in your table.`

Questions:
Answers:

My solution for this is using a new __construct method.

See:

class MyModel extends Eloquent {

    public $timestamps = false;

    public function __construct(array $attributes = [])
    {
            parent::__construct($attributes);

            $this->attributes['created_at'] = $this->freshTimestamp();
    }
}

Questions:
Answers:

A solution that is simple, decoupled, and reusable is to use a Model Observer. The idea is to capture the creating event and fill the created_at attribute. This method may be used by any number of models without having to repeat code or using unofficial tricks. Most importantly, it closely resembles the internal mechanics of the Model class, thus avoiding unexpected errors.

1) Create SetCreatedAt observer in App\Observers:

namespace App\Observers;

use Illuminate\Database\Eloquent\Model;

class SetCreatedAt
{
    /**
     * Sets created_at when creating the model.
     *
     * @param Model $model
     * @return void
     */
    public function creating(Model $model)
    {
        $model->setCreatedAt($model->freshTimestamp());
    }
}

2) On App\Providers\AppServiceProvider inside the boot method add the following line for each of the models that you want the created_at to be generated for:

/**
 * Bootstrap any application services.
 *
 * @return void
 */
public function boot()
{
    // Replace OrderLog with your model
    OrderLog::observe(SetCreatedAt::class);
}

3) On your models, the only thing that you have to do is disable the timestamps:

// Disable timestamps on the model
public $timestamps = false;

Tested with Laravel 5.3, but it should work with previous versions as well.

Good luck!

Questions:
Answers:

I solved by adding default value CURRENT_TIMESTAMP in my database for the column created_at. And In my model I use this below code

public $timestamps = false;
protected $dates = ['created_at'];

I hope this method works in all versions of laravel.

Questions:
Answers:

In 5.4, the problem it gives is that it also doesn’t populate updated_at field even after update (Eloquent update).

instead add this method

public function setUpdatedAtAttribute($value)
{
    // auto disable update_at in inserts
}

Questions:
Answers:

You can use CURRENT_TIMESTAMP default value for created field in your MySQL table and set

public $timestamps = false;

in your model.

Questions:
Answers:

I used very simple hack 😉

class MyClass extends Model {

const CREATED_AT = 'creation_date';
const UPDATED_AT = 'creation_date';

}

I just pointed both to the same column 🙂