1

I am new to Laravel.

I want to add timestamp for all insert actions in Laravel using custom field name such as ct and not using updated_at.

I came up with below, but without luck. All of my tables have ct field with not null property.

class ProductLog extends Model
{
    const CREATED_AT = 'ct';

    public $timestamps = false;

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

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

Both of 1) and 2) still throws me an error saying 'ct' doesn't have a default value.

Any suggestion or advice would be appreciated.

Thank you in advance.

smchae
  • 1,035
  • 3
  • 17
  • 39
  • did you have `ct` in the migration files ? – Maraboc Dec 27 '17 at 09:05
  • Possible duplicate of [how to only use created\_at in laravel](https://stackoverflow.com/questions/29886497/how-to-only-use-created-at-in-laravel) – online Thomas Dec 27 '17 at 09:10
  • @Maraboc No, I do not. Do I have to have that in migration file in order to use `static::creating`? – smchae Dec 27 '17 at 09:11
  • it depends on what you want you said _I want to add timestamp for all insert actions_ what do you mean add it to database or what ? – Maraboc Dec 27 '17 at 09:13
  • @Maraboc Sorry that I was not clear. I want to automatically insert a value ,current time, to `ct` field of tables when I insert certain row to the table. – smchae Dec 27 '17 at 09:18
  • So in this case you have to add it to the migration file ! and do it like this `$model->ct = $model->freshTimestamp();` – Maraboc Dec 27 '17 at 09:24
  • @Maraboc ok. So only when I add `ct` field using migration file, it would work. And if I created `ct` field manually using SQL script, it would not work. Did I understand it correctly? Is there any other way not to use migration and still achieve what I want? – smchae Dec 27 '17 at 09:32
  • @smchae you can use `const CREATED_AT = 'created_at';` from respective model and add `$timestamp false;` – Nikhil Radadiya Dec 27 '17 at 09:38
  • @smchae check my answer. I've just tested it and it works perfectly without any additional code. – Alexey Mezenin Dec 27 '17 at 09:54

3 Answers3

2

I've just tested this and it works perfectly:

const UPDATED_AT = null;
const CREATED_AT = 'ct';

There's no need to do anything else. With this solution, Laravel will use ct as created_at and will not add updated_at to any update queries.

Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • This answer does not work. It would give me an error saying ct does not have a default value, which means there is no value inserted for ct field. I am also using `protected $table = 'Table name'`. Will this affect the behavior? – smchae Dec 28 '17 at 00:27
  • @smchae try to remove all other code (listener, `timestamps = false` etc). It worked for me in a clean model. – Alexey Mezenin Dec 28 '17 at 07:39
  • It only works with `(new SomeModel)->save()`, not with query builder (e.g. `(new SomeModel)->insertGetId($data)`. Thank you for the advice. – smchae Dec 29 '17 at 09:15
  • @smchae that's because `insertGetId` is not an Eloquent method. Use `create`, it will use Eloquent and will return inserted object so you will be able to get it's ID. – Alexey Mezenin Dec 29 '17 at 09:18
1
class ProductLog extends Model
{
    const CREATED_AT = 'ct';

    public function setUpdatedAt($value)
    {
        return $this;
        // this method does nothing now
    }
}

Eloquent will set the created_at (ct) field for you on insert [as it does already when $timestamps = true;] and wont be able to set the update_at field now.

You can see what Eloquent is doing in the code Model@performInsert and Model@performUpdate.

lagbox
  • 48,571
  • 8
  • 72
  • 83
  • this is an alternate unneeded solution ... there is no need to override `setUpdateAt` as Alexey has pointed out, `updateTimestamps` checks to see if that const is null or not – lagbox Dec 27 '17 at 10:05
0

Try:

class ProductLog extends Model
{
    const CREATED_AT = 'ct';
    const UPDATED_AT = null;

    public $timestamps = false;

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

        static::creating(function ($model) {
            $model->ct = $model->freshTimestamp();
        });
    }
}
Gothiquo
  • 841
  • 1
  • 8
  • 31