在laravel 5.6中接管dingo/api 错误

有时候dingo/api返回的错误信息并不是我们需要的格式,我们需要自定义

在appProvidersAppServiceProvider类register加入以下方法即可,使用

use DingoApiFacadeAPI;

  public function register()
    {
      
        API::error(function (IlluminateValidationValidationException $exception){
            $data =$exception->validator->getMessageBag();
            $msg = collect($data)->first();
            if(is_array($msg)){
                $msg = $msg[0];
            }
            return response()->json(['data'=>$msg,'code'=>1], 200);
        });

    }

效果:

原文地址:https://www.cnblogs.com/fogwang/p/13171885.html