To remove the commas from the output of the code you provided, you can use json_encode() and json_decode():
function formatEmployee($record) {
$employee = Employee::select(['fname','name','mname'])
->where('id', $record->id)
->first()
->toArray();
$json = json_encode($employee);
$formatted = json_decode($json, true);
return $formatted;
}
json_encode() converts the array to a JSON string, which does not contain commas.
Then json_decode() converts it back to an associative array, without the commas.
So the returned data will be a clean associative array without commas between the keys and values.
You can also use implode() to join the values with another delimiter:
return implode(' ', $employee);
This would join the values with a space instead of commas.
So in summary, json_encode()/decode() or implode() can help format/clean the output without commas from the toArray() method.