laravel 自带的批量添加是不会自动维护时间戳的,意思是说,使用laravel的批量添加,时间戳字段不会插入,也不会在更新的时候进行更新。
使用继承或者trait来解决这个问题,在这里感谢Mr.wen
<?php
namespace App\Models\Traits;use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;trait BatchTraits
{public function storeBatch(array $inputs): bool{if (true === $this->timestamps) {$time = Date::now();foreach ($inputs as $key => $value) {if (! is_null(Model::CREATED_AT)) {$inputs[$key][Model::CREATED_AT] = $time;}if (! is_null(Model::UPDATED_AT)) {$inputs[$key][Model::UPDATED_AT] = $time;}}}$query = DB::connection($this->getConnectionName())->table($this->getTable());return $query->insert($inputs);}
}