Search code examples
phplaravellaravel-5laravel-5.5

Detected invalid UTF-8 for fieldname in laravel


I am storing some response from a third party. Sometimes data is not storing in to database. I checked the Laravel log and getting some error. I am using laravel 5.5 and mongo db.

Any help world be appreciated. Thanks.

Laravel log

[2018-09-10 11:40:50] production.ERROR: Detected invalid UTF-8 for fieldname "shipping_city": München  
[2018-09-10 11:41:03] production.ERROR: Detected invalid UTF-8 for fieldname "_id": Ã$zjmêá¹ëmy×
[2018-09-10 11:48:16] production.ERROR: Method [error] does not exist on [App\Http\Controllers\PaymentController]. {"userId":"5b0d2b6f12236ca36a2282ed","exception":"[object] (BadMethodCallException(code: 0): Method [error] does not exist on [App\\Http\\Controllers\\PaymentController]. at /var/www/vhosts/cabin-holiday.frontend/vendor/laravel/framework/src/Illuminate/Routing/Controller.php:68)

PaymentController.php

$payment        = new Payment;

   foreach($_POST as $key => $value) {
     if(Schema::hasColumn($payment->getTable(), $key)){
        if(is_array($value)) {
           $payment->{$key} = $value[1];
        } else {
                $payment->{$key} = $value;
                }
      }
    }
$payment->save();

Solution

  • Couple of ways you can handle that. One of them is to use utf8-encode

    string utf8_encode ( string $data )
    

    PaymentController.php

    $payment = new Payment;
    
       foreach($_POST as $key => $value) {
         if(Schema::hasColumn($payment->getTable(), $key)){
            if(is_array($value)) {
               $payment->{$key} = utf8_encode($value[1]);//changes
            } else 
            {
                        $payment->{$key} = utf8_encode($value);//changes
    
            }
    
            }
            }
        $payment->save();