text: Q:
  
  JSON errors of LoginController and RegisterController should be strings not arrays
  
  I am using Laravel 5.5.14 (php artisan --version). I am using Laravel to create just a REST API.
  When I get errors, the shape of it looks like this:
  [
      'message' => 'The given data was invalid.',
      'errors' => [
          'email' => ['The email field is required.'],
          'password' => ['The password field is required.']
      ]
  ]
  
  My 
  We see inside the errors array, each field (email, password) instead of being strings, is an array of a single string. Can there ever be more then one error is this why it is an array? Even if there is more then one error, I wanted to tell Laravel just report the first error without an array, is this possible?
  In my api.php I have this:
  Route::post('login', 'Auth\LoginController@login');
  Route::post('logout', 'Auth\LoginController@logout');
  Route::post('register', 'Auth\RegisterController@register');
  
  My LoginController.php looks like this:
  public function __construct()
  {
      $this->middleware('guest')->except('logout');
  }
  
  public function login(Request $request)
  {
      $this->validateLogin($request);
  
      if ($this->attemptLogin($request)) {
          $user = $this->guard()->user()->load('pets');
          $user->generateToken();
  
          return $user;
      }
  
      return $this->sendFailedLoginResponse($request);
  }
  
  public function logout(Request $request)
  {
      $user = Auth::guard('api')->user();
  
      if ($user) {
          $user->api_token = null;
          $user->save();
      }
  
      return ['ok' => true];
  }
  
  And my RegisterController.php looks like this:
  public function __construct()
  {
      $this->middleware('guest');
  }
  
  /**
   * The user has been registered.
   *
   * @param  \Illuminate\Http\Request  $request
   * @param  mixed  $user
   * @return mixed
   */
  protected function registered(Request $request, $user)
  {
      $user->generateToken();
  
      return response()->json($user, 201);
  }
  
  /**
   * Get a validator for an incoming registration request.
   *
   * @param  array  $data
   * @return \Illuminate\Contracts\Validation\Validator
   */
  protected function validator(array $data)
  {
      return Validator::make($data, [
          'name' => 'required|string|max:255',
          'email' => 'required|string|email|max:255|unique:users',
          'password' => 'required|string|min:6|confirmed',
      ]);
  }
  
  /**
   * Create a new user instance after a valid registration.
   *
   * @param  array  $data
   * @return \App\User
   */
  protected function create(array $data)
  {
      return User::create([
          'name' => $data['name'],
          'email' => $data['email'],
          'password' => bcrypt($data['password']),
      ]);
  }
  
  A:
  
  Override validateLogin to the following or you could give it a different name and call it from your controller function-
  public function validateLogin($request){
    $rules = [
          'user_name' => 'required|string',
          'password' => 'required|string',
      ];
      $validator = Validator::make($request->all(), $rules);
  
      if ($validator->fails()) {
          $errorResponse = $this->validationErrorsToString($validator->errors());
          return response()->json(['message' => 'The given data was invalid.', 'errors' => $errorResponse],400);
      }
  }
  
  And make another function to transform your response to expected format with this method-
  private function validationErrorsToString($errArray) {
      $valArr = array();
      foreach ($errArray->toArray() as $key => $value) {
          $valArr[$key] = $value[0];
      }
      return $valArr;
  }
  
  And don't forget to add this at the start of your controller file-
  use Validator;
  
  Hope this helps you :)
  
  