I want to return my user's data with a specific field formatted differently.
Users have a field some_field
which contains a string, let's say abcdef
. I want to format it like so : ab cd ef
- which I can do using wordwrap($user->some_field, 2, ' ', true);
So I'm doing:
public function dashboard(Request $request) {
$request->user()->some_field = wordwrap($request->user()->some_field, 2, ' ', true);
return view('dashboard', [
'user' => $request->user(),
]);
}
Is it possible to apply this somehow for each and every time a user's data is returned, or do I have to make it happen in every controller returning user's data?
In your model you have to define accessor have a look here: Defining An Accessor
In your case it will look like this: