John Davidson

php - Words with unconfigured accent using JSON column in Laravel

0 comments
Message:


I'm having a problem with accenting (UTF-8) words in the data collected from the JSON column properties.



  • Actually: "type_nen": "Emiss\u00e3o"

  • Expected: "type_nen": "Emissão" (I want that be showed)


    public function store(HunterRequest $request)
{
$validations = $request->validated();
$validations['properties'] = $validations;
HunterModel::create($validations);
return redirect('/')->with('success_store','Hunter are registered in the system.');
}


My Answer:
To properly handle UTF-8 characters and accents in Laravel when storing data in a JSON column, you can use the `json_encode` function with the `JSON_UNESCAPED_UNICODE` option to ensure that the special characters are stored correctly.

Here's an updated version of your `store` method that includes this modification:

php
public function store(HunterRequest $request)
{
$validations = $request->validated();
$validations['properties'] = json_encode($validations, JSON_UNESCAPED_UNICODE);
HunterModel::create($validations);
return redirect('/')->with('success_store','Hunter are registered in the system.');
}


By using `json_encode` with the `JSON_UNESCAPED_UNICODE` option, the UTF-8 characters will be stored as-is in the JSON column without being escaped, allowing them to be displayed correctly with accents.

Rate this post

3 of 5 based on 5456 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us