从 angular 发送数据时 laravel 中的日期时间格式无效
Invalid datetime format in laravel while sending data from angular
谁能帮我解决这个问题?我有一个来自 angular 的请求数据,我已将其传递给 laravel 后端,以便一次插入多行。但是我遇到了以下错误。我在 Mysql 中尝试了 运行 下面的查询,它在那里工作正常但在 laravel.
中没有
我该如何解决这个问题?
从 Angular (API) 请求数据:
[
{
"customer_id": 3,
"check_in_date": "2020-07-30T00:00:00.000Z",
"check_out_date": "2020-07-31T00:00:00.000Z",
"room_id": 2
},
{
"customer_id": 3,
"check_in_date": "2020-07-29T00:00:00.000Z",
"check_out_date": "2020-07-31T00:00:00.000Z",
"room_id": 3
}
]
迁移Table
public function up()
{
Schema::create('reservations', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('room_id');
$table->foreign('room_id')->references('id')->on('rooms');
$table->unsignedBigInteger('customer_id');
$table->foreign('customer_id')->references('id')->on('customers');
$table->unsignedBigInteger('booking_id')->nullable();
$table->foreign('booking_id')->references('id')->on('bookings');
$table->dateTime('check_in_date');
$table->dateTime('check_out_date');
$table->timestamps();
});
}
预订管理员:
public function store(Request $request)
{
$reservation = Reservation::insert($request->all());
return $this->jsonResponse(true, 'Reservation has been created successfully.', $reservation);
}
private function jsonResponse($success = false, $message = '', $data = null)
{
return response()->json([
'success' => $success,
'message' => $message,
'data' => $data
]);
}
错误:
"message": "SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '2020-07-
29T00:00:00.000Z' for column 'check_in_date' at row 1
(SQL: insert into `reservations` (`check_in_date`, `check_out_date`, `customer_id`, `room_id`)
values (2020-07-30T00:00:00.000Z, 2020-07-31T00:00:00.000Z, 3, 2),
(2020-07-29T00:00:00.000Z, 2020- 07-31T00:00:00.000Z, 3, 3))",
"exception": "Illuminate\Database\QueryException",
日期格式错误,请尝试使用 Carbon
转换它们,它应该会自动序列化为您的 DBMS 所需的正确格式:
use Carbon\Carbon;
...
function store(Request $request)
{
$all = array_map(function($el){
$el->check_in_date = Carbon::createFromFormat('Y-m-d\TH:i:s+', $el->check_in_date);
$el->check_out_date = Carbon::createFromFormat('Y-m-d\TH:i:s+', $el->check_out_date);
return $el;
}, $request->all());
$reservation = Reservation::insert($all);
return $this->jsonResponse(true, 'Reservation has been created successfully.', $reservation);
}
编辑:访问对象字段似乎存在一些问题,已使用 $el['check_in_date']
而不是 $el->check_in_date
解决
谁能帮我解决这个问题?我有一个来自 angular 的请求数据,我已将其传递给 laravel 后端,以便一次插入多行。但是我遇到了以下错误。我在 Mysql 中尝试了 运行 下面的查询,它在那里工作正常但在 laravel.
中没有我该如何解决这个问题?
从 Angular (API) 请求数据:
[
{
"customer_id": 3,
"check_in_date": "2020-07-30T00:00:00.000Z",
"check_out_date": "2020-07-31T00:00:00.000Z",
"room_id": 2
},
{
"customer_id": 3,
"check_in_date": "2020-07-29T00:00:00.000Z",
"check_out_date": "2020-07-31T00:00:00.000Z",
"room_id": 3
}
]
迁移Table
public function up()
{
Schema::create('reservations', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('room_id');
$table->foreign('room_id')->references('id')->on('rooms');
$table->unsignedBigInteger('customer_id');
$table->foreign('customer_id')->references('id')->on('customers');
$table->unsignedBigInteger('booking_id')->nullable();
$table->foreign('booking_id')->references('id')->on('bookings');
$table->dateTime('check_in_date');
$table->dateTime('check_out_date');
$table->timestamps();
});
}
预订管理员:
public function store(Request $request)
{
$reservation = Reservation::insert($request->all());
return $this->jsonResponse(true, 'Reservation has been created successfully.', $reservation);
}
private function jsonResponse($success = false, $message = '', $data = null)
{
return response()->json([
'success' => $success,
'message' => $message,
'data' => $data
]);
}
错误:
"message": "SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '2020-07-
29T00:00:00.000Z' for column 'check_in_date' at row 1
(SQL: insert into `reservations` (`check_in_date`, `check_out_date`, `customer_id`, `room_id`)
values (2020-07-30T00:00:00.000Z, 2020-07-31T00:00:00.000Z, 3, 2),
(2020-07-29T00:00:00.000Z, 2020- 07-31T00:00:00.000Z, 3, 3))",
"exception": "Illuminate\Database\QueryException",
日期格式错误,请尝试使用 Carbon
转换它们,它应该会自动序列化为您的 DBMS 所需的正确格式:
use Carbon\Carbon;
...
function store(Request $request)
{
$all = array_map(function($el){
$el->check_in_date = Carbon::createFromFormat('Y-m-d\TH:i:s+', $el->check_in_date);
$el->check_out_date = Carbon::createFromFormat('Y-m-d\TH:i:s+', $el->check_out_date);
return $el;
}, $request->all());
$reservation = Reservation::insert($all);
return $this->jsonResponse(true, 'Reservation has been created successfully.', $reservation);
}
编辑:访问对象字段似乎存在一些问题,已使用 $el['check_in_date']
而不是 $el->check_in_date