亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從 Angular 發送數據時,laravel 中的日期時間格式無效

從 Angular 發送數據時,laravel 中的日期時間格式無效

PHP
慕的地8271018 2023-09-08 17:21:03
誰能幫助我如何解決這個問題?我有一個來自 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",
查看完整描述

1 回答

?
蝴蝶不菲

TA貢獻1810條經驗 獲得超4個贊

日期的格式錯誤,嘗試使用 轉換它們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


查看完整回答
反對 回復 2023-09-08
  • 1 回答
  • 0 關注
  • 114 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號