search

Home  >  Q&A  >  body text

PHP framework Laravel Eloquent ORM batch insert data, creation time 00000000000

foreach ($products as $v=>$a)
{
    $count[] = array('product_name' => $a['name'], 'product_weight' => $a['weight'], 'product_id' => $a['pid'], 'product_price' => $a['price'], 'order_id' => $order->id, 'card_phone' => $user->phone);
}
CountOrder::insert($count);

When using laravel to insert data in batches,

The creation time of the inserted data is all 0000000;

How to save the creation time and update time fields normally? Do they also need to be written in the array?

大家讲道理大家讲道理2779 days ago781

reply all(3)I'll reply

  • 黄舟

    黄舟2017-05-16 16:57:45

    把updated_at字段属性改成下面这样,
    `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    
    当然`created_at`是要自己赋值的,不然就会插入默认值0000-00-00 00:00:00
    

    reply
    0
  • 高洛峰

    高洛峰2017-05-16 16:57:45

    In fact, you can use save()

    $this->fill($count)->save();
    

    My laravel tutorial column: /u/biaoyansu/blogs

    reply
    0
  • 大家讲道理

    大家讲道理2017-05-16 16:57:45

    Laravel maintains created_at updated_at by default and needs to be declared in the model public $timestamps = false; the default is true

    class Student extends Model
    {
        protected $table = 'student';//指定表名
        //protected $primaryKey = 'id';//id
        //自动维护时间戳 默认是true;
        //public $timestamps = false;
    
        protected function getDateFormat()
        {
            return time();//return当前时间戳
            //输出的时候已经帮我们格式化了不想格式化需加函数asDateTime()
        }
    
       /* protected function asDateTime($val)
        {
            return $val;//将$val  return回去 不做任何处理就不会格式化了
        }*/
    }

    reply
    0
  • Cancelreply