How to convert my json view in laravel?

Posted 2 months ago by raviawasti

while using validation in controller

$data = $request->all();

   $validator = Validator::make($data, [
       'username' => 'required|numeric|digits:10|unique:users',
    'name' => 'required|max:80|regex:/^[\pL\s\-]+$/u',
    'email' => 'required|email|max:60|unique:users',
    ]);

   
  if ($validator->fails())
  {
  return response()->json(['success'=>'0','post'=>$validator->errors()]);
  }

i am getting data like this(i want to remove [ ] from first one)

{ "success": "0", "post": { "username": [ "The username has already been taken." ], "email": [ "The email has already been taken." ] } }

but i want in this format

{ "success": "0", "post": { "username": "The username has already been taken", "email": "The email has already been taken." } }

Please sign in or create an account to participate in this conversation.