PHP framework Laravel Eloquent ORM batch insert data, creation time 00000000000
大家讲道理
大家讲道理 2017-05-16 16:55:45
0
3
756
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?

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

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

In fact, you can use save()

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

My laravel tutorial column: /u/biaoyansu/blogs

大家讲道理

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回去 不做任何处理就不会格式化了
    }*/
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!