Problems with charset in the Laravel

Asked

Viewed 1,580 times

2

I have a database Firebird which was not built through migrations in the Laravel, and it has encoding ISO-8859-1 and I need to return an object in the format JSON for my application, however, in doing so, the following error occurs:

"The Response content must be a string of Object implementing ___toString(), "Boolean" Given."

This mistake happens because objects JSON need to be constructed from strings UFT-8. However, I consider it unnecessary to have to do this type of conversion at all times, and my application to lose performance because of it.

I’m using several Models of Laravel, however, there is some specific method where I can do this type of conversion without having to create loop and convert manually?

  • put all the code related to the error, and the problem? because it has errors and problems in your inquiry? model, tabela e seu layout are welcome, I even imagine the solution, but, I need to visualize where to put !

2 answers

0

You can use the Accessors & Mutators of , where field formatting is done directly model:

I will propose an example, because in your question there is no model and it would be important to have and the table layout because of the fields, but I’m going to generalize with a basic example.

Example:

In your specific case the display factor is what you need:

<?php

namespace App;    
use Illuminate\Database\Eloquent\Model;

class Noticias extends Model
{    
    public function getTituloAttribute($value)
    {
        return strtoupper($value);
    }
}

Inside the table noticias, has a field with the name title, I need the display to be all capitalized, so create a mutation method get which automatically does the specific field conversion title.

Standards:

get + field name + Attribute($value)

if the field is separate for underscore as an example primeiro_nome would be:

public function getPrimeiroNomeAttribute($value)

that is, the initials in capital letters between underscore


There is also the set that would be a change of the value sent, also putting effects and significant changes in the value of this field, reflecting all this change in its table.

Standards:

set + field name + Attribute($value)

Code with the same field titulo table noticias (fictional example):

public function setTituloAttribute($value)
{
     $this->attributes['titulo'] = $value;
}

Complete code fictional example Model Noticias:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Noticias extends Model
{    
    // GET
    public function getTituloAttribute($value)
    {
        return strtoupper($value);
    }
    // SET
    public function setTituloAttribute($value)
    {
        $this->attributes['titulo'] = $value;
    }
}

0

In my case I solved in the Laravel config database, setting the collation = iso and charset = utf8

    'oracle' => [
        'driver' => 'oracle',            
        'host' => 'seu host',
        'port' => 'porta',
        'database' => 'database',
        'username' => 'user',
        'password' => 'senha',
        'charset' => 'utf8',
        'collation' => 'WE8ISO8859P1',
        'prefix' => '',
    ],

Browser other questions tagged

You are not signed in. Login or sign up in order to post.