Home > database >  created_at and updated_at columns not updated by updateorcreate - Laravel Eloquent
created_at and updated_at columns not updated by updateorcreate - Laravel Eloquent

Time:10-03

Hey All I am new to Laravel. I have a table called dsp_account_fee. enter image description here enter image description here

This is what I have in my Model.

{
    use AdvancedInserts;

    protected $table = "dsp_account_fee";
    protected $primaryKey = 'dsp_account_id';
    protected $secondaryKey = 'screen_type';


    public $timestamps = false;

    protected function setKeysForSaveQuery(Builder $query)
    {
        parent::setKeysForSaveQuery($query);
        $query->where($this->secondaryKey, '=', $this->{$this->secondaryKey});
        return $query;
    }

    protected $fillable = array(
        'dsp_account_id', 'screen_type', 'pmp_percent' , 'omp_percent'
    );

}

When I create or update record I use the updateorcreate method like this:

$res1 = DspAccountFee::updateOrCreate(
                ['dsp_account_id' => $dsp_account_id, 'screen_type' => 'ctv'],
                ['pmp_percent' =>$fields['fee_ctv_pmp_percent'], 'omp_percent' => $fields['fee_ctv_omp_percent']]
                );

The problem is that the created_at and the updated_at not filled automatically and stay null. what I am missing?

CodePudding user response:

Set it to true or remove it as Laravel by default set it to true.

public $timestamps = true;

CodePudding user response:

I think you need to change $timestamps to true

public $timestamps = true;

CodePudding user response:

Remove this from Model

public $timestamps = false;
  • Related