Laravel catch Validationexception in custom request
<p>I'm building an API in Laravel, and am using a custom request to validate the inbound data. My problem is that I'm not sure how I 'catch' the validation errors to shape the response.</p>

<p>Here's what I have so far.</p>

<p>Register method wrapped in a transaction:</p>

<pre><code> //Create User Request extends standard request. Handles Validation

  public function __construct(CreateUserRequest $request){
        $this-&gt;request = $request;
  }

  public function register()
  {
    try{

      $array = DB::transaction(function(){

            $email = $this-&gt;request-&gt;input('email');
            $password = $this-&gt;request-&gt;input('password');
            $companyName = $this-&gt;request-&gt;input('companyName');
            $userName = $this-&gt;request-&gt;input('name');
            $country = $this-&gt;request-&gt;input('country');

            $company = Company::create([
                'name' =&gt; $companyName,
                'active'=&gt;true,
                'country_id'=&gt;$country
            ]);

            $user = User::create([
                'company_id' =&gt; $company-&gt;id,
                'name'=&gt;'admin',
                'email' =&gt; $email,
                'password' =&gt; $password,
                'active' =&gt;true
            ]);

            if( !$company || !$user )
            {
                throw new \Exception('User not created for account');
            }

            return compact('company', 'user');
          });

        $token = JWTAuth::fromUser($array['user']);
        return Response::json(compact('token'));

    }
    catch( Exception $e )
    {
        return Response::json(['error' =&gt; $e-&gt;getMessage() ],  HttpResponse::HTTP_CONFLICT );
    }

  }
</code></pre>

<p>The form request looks like this:</p>

<pre><code>class CreateUserRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
      return [
          'email' =&gt; 'required|unique:users',
          'password' =&gt; 'required',
          'companyName' =&gt; 'required',
          'name' =&gt; 'required',
          'country' =&gt; 'required|numeric'
      ];
    }
}
</code></pre>

<p>My errors are coming back automatically, which appear to be the messagebag object serialised to JSON</p>

<pre><code>{"email":["The email has already been taken."]}{"email":["The email has already been taken."]}
</code></pre>

<p>Somewhere in there I need to catch the Exception inside the main Controller, but I've used the Custom Request Class to clean up my controller a bit, how would I do that? Note the Exception already caught in this controller, which doesn't seem to pickup whatever is thrown behind the scenes in the custom request. </p>

<p>any pointers? do I need to move validation back to the controller? or is there a cleaner way to do this? </p>