laravel-mongodb icon indicating copy to clipboard operation
laravel-mongodb copied to clipboard

Pass custom timestamps to new document / update timestamps on existing document

Open ordinov opened this issue 4 years ago • 1 comments

  • Laravel-mongodb Version: 3.8
  • PHP Version: 7.4

Description:

Can't change created_at and updated_at fields

Steps to reproduce

Model::create(['name' =>' something', 'created_at' => '2020-01-01 00:00:00', 'updated_at' => '2020-01-01 00:00:00']); OR $createdDocument->update([ 'created_at' => '2020-01-01 00:00:00', 'updated_at' => '2020-01-01 00:00:00']);

Expected behaviour

I'd like to see the dates/timestamps i actually passed, as you can do with Eloquent. This is strictly necessary if you are going to move some table contents from mysql to mongo like: foreach (OldModel::all() as $record) { unset($record->id); NewModel::create($record->toArray()); }

Actual behaviour

created_at and updated_at are set as current datetime

ordinov avatar Nov 27 '20 03:11 ordinov

try this in your eloquent model

     /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;

Trait inhered in base model from Illuminate\Database\Eloquent\Concerns\HasTimestamps

exemplari avatar Jun 25 '22 11:06 exemplari