![](/img/trans.png)
[英]Incorrect date value when trying to save date to Mysql from nodejs
[英]Laravel - POST date as JSON and save as DATE Constraint in MySQL
我在 MySQL Column DataType Constraint 數據庫中定義了DATE
格式。
但是,在以ISO
格式發送 JSON object 中的日期時,它必須是 MySQL 中的DATE
數據類型使用 Laravel質量分配和所有輸入數據進行約束
SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect date value: '1990-01-01T00:00:00.000Z' for column 'date_of_birth' at row 1
刀片銼刀
<input type="date" id="date_of_birth">
<script>
var dob = new Date( $("#date_of_birth").val() ).toISOString();
</script>
郵寄 JSON object
{
date_of_birth: "1990-01-01T00:00:00.000Z"
}
批量分配的設置屬性
App\Models\User.php
protected $fillable = [ 'date_of_birth' ]
App\Http\Controllers\UpdateController.php
public function update(Request $request, User $user)
{
$id = $user->updateOrCreate($request->all());
我想感謝@Armagen 。 使用突變器。
use Carbon\Carbon;
class User extends Model
{
/**
* Set the user's date_of_birth.
*
* @param string $value
* @return void
*/
public function setDateOfBirthAttribute($value)
{
$this->attributes['date_of_birth'] = Carbon::parse($value);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.