Laravel + Postgres - Return’t' or 'f' instead of true or false

Asked

Viewed 88 times

0

Hello,

When performing a query in a field booleano in the Postgres we have a return of 't' or 'f'.

I’m having a problem trying to migrate an API made in Codeigniter for an API I am developing in Laravel 5.7. The problem is that the Codeigniter returns 't' or 'f' in consultations, and Laravel returns true or false. I need to let the return of Laravel equal to that of the Codeigniter, because it is not feasible for me to touch the Front-End that consumes this API.

It would be ideal for me if it were possible to configure this in just one place of my application, without having to change all the queries that I am doing. It is also important to point out that I am not using the Eloquent, I’m using the Query Builder of Laravel

Example of the return of Codeigniter [Line 5]

{
  "id": "5794",
  "valor_frete": 19.8,
  "prazo_entrega": 6,
  "exibe_prazoentrega": "t",
  "destino": "RJ - Rio de Janeiro - Capital",
  "transportadora": "Correios",
  "nome": "PAC - Encomenda Normal",
  "texto": "PAC - Encomenda Normal",
  "frete_gratis": "Frete Grátis",
  "valor_frete_original": 19.8
},

Example of the return of Laravel [Line 7]

{
  "id": 5794,
  "nome": "PAC - Encomenda Normal",
  "texto": "PAC - Encomenda Normal",
  "destino": "RJ - Rio de Janeiro - Capital",
  "transportadora": "Correios",
  "exibe_prazoentrega": true,
  "valor_frete": 19.8,
  "prazo_entrega": 6,
  "valor_frete_original": 19.8
},

Thank you.

  • how is it in the database ? that same line ?

  • If you have to create the same access modifier or even an append are things to think about. In Codeingniter as the guy did?

  • In Codeigniter it comes by default

  • Sorry Jhonny how? if you can show this in your question? because if it is a given char for example in Laravel should also, maybe we need to understand how it is being recorded at the base if it has to demonstrate?

1 answer

1

You can add this to the Model

public function getExibePrazoentregaAttribute($value)
{
    return $value?'t':'f';
}

That is, whenever you regain the field exibe_prazoentrega, he’s gonna get into getExibePrazoDelivery and check if it is true, will return t if the f

Any questions edit your post with your Model that I update here.

  • I had thought about it, but I would have to do it a few hundred times, the API is giant HEHE

  • Well, and you can make a rum script that adds these lines to all your Models.

  • Tomorrow when I go to work I’ll see it and let you know the result.

Browser other questions tagged

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