I am a beginner using laravel
I'm trying to return the data stored in DB with json
When it outputs, it is garbled.
I want to avoid garbled characters

Error message

User information is stored in DB
It is not garbled on DB, but if you output with json_encode ()

"id": 1, "user_id": 1, "name": "\ u3055 \ u304f \ u3089 \ u306f \ u306a \ u3048", "sex": 2, "age": 36, " work ":" \ u516c \ u52d9 \ u54e1 "......

Will be garbled. (Name and work include "Tanaka Tarou" and "Public employee" in Japanese on the DB)

Applicable source code
public function show (Profile $profile)
        $profilejson = json_encode ($profile);
        return view ('profiles.show', compact ('profile', 'profilejson'));
If i output as garbled characters will appear.
Supplemental information (FW/tool version etc.)

laravel5.6, using resource controller.

  • Answer # 1

    Specification of json_encode.
    As long as you pass it to javascript as it is, you can use it as it is
    (Returns properly when decoding)

    I really want to display Japanese

    json_encode ($profile, JSON_UNESCAPED_UNICODE)

    will display Unicode without encoding

Related articles