I want to convert laravel validation error array to a comma separated string. This is to use in an api service for an ios application. So that the iOs developer can process error messages easily.
I tried,
$valArr = [];
foreach ($validator->errors() as $key => $value) {
$errStr = $key.' '.$value[0];
array_push($valArr, $errStr);
}
if(!empty($valArr)){
$errStrFinal = implode(',', $valArr);
}
But it is not working.
You should do like this :
P.S. Assuming
The
$validator->errors()
returns aMessageBag
,see: https://laravel.com/api/5.3/Illuminate/Support/MessageBag.html.
You are close, you need to call the
getMessages()
function onerrors()
, so:Hope this helps :)