Carbon (laravel) deal with invalid date

前端 未结 4 1947
面向向阳花
面向向阳花 2021-02-20 05:22

I have a quite simple problem.. I use the Carbon::parse($date) function with $date = \'15.15.2015\'. Of course it can not return a valid string because

4条回答
  •  北荒
    北荒 (楼主)
    2021-02-20 06:10

    another solution would be to handle the exception globally.

    • in app/Exceptions/Handler.php
    use Carbon\Exceptions\InvalidFormatException;
    
    • then in the render method check if an exception is thrown by Carbon
        if ($exception instanceof InvalidFormatException) {
                return response()->json([
                    'status' => 'fail',
                    'data' => [
                        'message' => 'Invalid date formate!'
                    ]
                ], 400);
            }
    

提交回复
热议问题