Php 从数据库发送数据时,laravel中的日期时间格式无效

Php 从数据库发送数据时,laravel中的日期时间格式无效,php,laravel,laravel-5,eloquent,associative-array,Php,Laravel,Laravel 5,Eloquent,Associative Array,谁能帮我解决这个问题?我有一个来自angular的请求数据,我已将其传递到laravel后端,以便一次插入多行。但我犯了以下错误。我试着在Mysql中运行下面的查询,它在那里运行得很好,但不是从laravel 我怎样才能解决这个问题 从Angular API请求数据: [ { "customer_id": 3, "check_in_date": "2020-07-30T00:00:00.000Z", &quo

谁能帮我解决这个问题?我有一个来自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
  }
]
迁移表

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'

解决了这些问题。这些是atom日期吗?我尝试在Mysql中运行下面的查询,效果很好,但您没有运行精确的查询。如果您向我们显示与错误查询匹配的输入数据,这也会有所帮助。2个输入不产生3个输出,且日期不同。这一切都很重要Mysql希望日期的格式为2020-07-29 00:00:00.000,因此通过DateTime或Carbon调用运行日期以正确地重新格式化是的,在Mysql中查询工作正常,但在through laravel应用程序中我发现了上述错误。消息:尝试获取非对象的属性“check_in_date”,异常:ErrorException检查了请求的类型gettype$request。。。它返回我objectI将$el->check_in_date更改为$el['check_in_date']。。。。退房日期也是如此。谢谢你@Berto99。。。现在对我有用了。@susttarai对不起,我不在家。。。这很奇怪,因为根据你在问题上的表现,el应该包含对象