If you are doing it like me without your validator and you are pulling messages from the exception you can use laravel helper Arr::flatten($array);
Link and code are for laravel 8.x but I tested this with 5.7 ;) It works.
From documentation:
use Illuminate\Support\Arr;
$array = ['name' => 'Joe', 'languages' => ['PHP', 'Ruby']];
$flattened = Arr::flatten($array);
// ['Joe', 'PHP', 'Ruby']
My code:
try {
$request->validate([
'test1' => 'required|integer',
'test2' => 'required|integer',
'test3' => 'required|string',
]);
} catch (ValidationException $validationException) {
return response()->json([
'type' => 'error',
'title' => $validationException->getMessage(),
'messages' => Arr::flatten($validationException->errors())
], $validationException->status);
} catch (\Exception $exception) {
return response()->json([
'type' => 'error',
'title' => $exception->getMessage(),
], $exception->getCode());
}
As you can see I am pulling the message and setting it as my title. Then I am using Arr::flatten($validationException->errors()) to get the validation messages and but to flatten my array for SweetAlert2 on the frontend.
I know I am late but I hope it will help someone that comes across these problems.
Greetings! :)