laravel-auditing icon indicating copy to clipboard operation
laravel-auditing copied to clipboard

Unable to encode attribute [new_values] for model [OwenIt\Auditing\Models\Audit] to JSON: Malformed UTF-8 characters, possibly incorrectly encoded.

Open NMFES opened this issue 5 years ago • 2 comments

Q A
Bug? yes
New Feature? no
Framework Laravel
Framework version 5.7.19
Package version 8.0.4
PHP version 7.1.17

Actual Behaviour

$listing = Listing::where('ListingID', 1758221)->first();
$content = file_get_contents('test.txt');
$listing->AdvertisingText = $content;
$listing->save();

This code throws an error:

In JsonEncodingException.php line 33:
  Unable to encode attribute [new_values] for model [OwenIt\Auditing\Models\Audit] to JSON: Malformed UTF-8 characters, possibly incorrectly encoded. 

Expected Behaviour

Value of $content contains non utf-8 symbols. So it should be converted to utf-8 or these special chars should be removed to avoid errors appearing

Steps to Reproduce

File with non utf-8 char http://rgho.st/private/6MgdG6Z7Z/db25b52f2db92ed0d3af98ea6e38191e

I use MSSQL DB. But You can create equal table with varchar/text column in MySQL. Also I removed from SQL other columns.

CREATE TABLE Listings (
	ListingID int NOT NULL,
	AdvertisingText varchar(200) NULL
) GO

Error happens in vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php in castAttributeAsJson method.

Possible Solutions

NMFES avatar Jun 20 '19 00:06 NMFES

Importing non-utf8 characters into a utf8 environment is sure to cause problems.

Audit might not be able to help because it just uses the text you supply in the model.
Can you convert the symbols calling PHP's mb_convert_encoding() before saving?

Using several encodings at the same time must be troublesome. Maybe you need an encoding column and flip-flop between encodings as you need?
If mb_convert_encoding() or similar helper functions for encoding can help out you should try first.

You can also toss around utf8_decode() and utf8_encode() calls just to test if this fixes anything.

Or do you want to support those non-utf8 characters?

Peter-Krebs avatar Sep 18 '19 15:09 Peter-Krebs

I am getting this same issue when inserting model record but on updating model record it is working fine.

webosdeveloper avatar Jun 04 '21 05:06 webosdeveloper

Any solution yet about this issue? Im also getting this issue currently

nazrinrahman097 avatar Oct 13 '22 07:10 nazrinrahman097

json cannot handle all bytes of a bin or file contents, try adding a base64_encode, base64_decode cast, example:

$listing = Listing::where('ListingID', 1758221)->first();
$content = file_get_contents('test.txt');
$listing->AdvertisingText = base64_encode($content);
$listing->save();

parallels999 avatar Mar 13 '23 15:03 parallels999