mongodb / laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel (Moloquent)
https://www.mongodb.com/docs/drivers/php/laravel-mongodb/
MIT License
7k stars 1.43k forks source link

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

Open ordinov opened 3 years ago

ordinov commented 3 years ago

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

exemplari commented 2 years ago

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