laravel作为后台api使用,如何使用$request->validate([])验证

修改app/Exceptions/Handler.php文件内容

use Illuminate\Validation\ValidationException;

修改render函数

public function render($request, Throwable $exception)
    {
        if ($exception instanceof ValidationException) {
            return response()->json([
                'code' => 422,
                'msg' => '调用参数错误',
                'data' => $exception->errors(),
            ], 422);
        }
        return parent::render($request, $exception);
    }

结果:

猜你喜欢

转载自www.cnblogs.com/roland-sky/p/12905196.html