Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mskhoshnazar's avatar

Getting a utf8 decoded string when retrieving session data

I'm trying to log some data in a middleware, and I'm storing the json that session('errors') returns in database. But all unicode characters are escaped.

I've made it work with a workaround but it feels very hacky.

Is there a recommended way of getting utf8 strings out of session?

0 likes
7 replies
mskhoshnazar's avatar

where is json_encode? are you suggesting i change laravel code?

skliche's avatar

It's a part of PHP, see the link I provided.

mskhoshnazar's avatar

I know the function dude :D I'm not using json_encode. I'm just calling session. You might wanna take another look at the question.

skliche's avatar

You are not "just" calling session. session('errors') does not return JSON. It returns a ViewErrorBag. When you use something like logger(session('errors')); the log writer tries to create a string representation for the contents. ViewErrorBag is Jsonable, so the method toJson is called on it. Now guess what toJson calls ... json_encode.

mskhoshnazar's avatar

so you're suggesting i change framework code as i said. I would really appreciate it if you read my input and make a clear, specific suggestion about this particular problem instead of giving 101 information on php functions and how laravel works.

skliche's avatar
skliche
Best Answer
Level 42

No, I'm suggesting to use the option JSON_UNESCAPED_UNICODE:

logger(session('errors')->toJson(JSON_UNESCAPED_UNICODE))

instead of

logger(session('errors'))
1 like

Please or to participate in this conversation.