Organize json with Eloquent

Asked

Viewed 48 times

1

My question is this: I want to organize my json for me to consult the table Animal and Hacienda, but organized, example:

{
"animais": {
    "id": 1,
    "nome": "Mimosa",
    "data_nascimento": "2017-04-11 00:00:00",
    "codigo_brinco": "Não informado",
    "codigo_raca": "Não informado",
    "status": 1,
    "data_alteracao": "2018-02-13 00:00:00",
    "data_cadastro": "2018-02-13 00:00:00",
    "usuario_cadastro": 1,
    "usuario_alteracao": null,
    "fazendas_id": 1,
    "lotes_id": 1,
    "fazenda": {
        "id": 1,
        "nome": "Fazenda Nossa Senhora aparecida",
        "data_alteracao": "2018-02-13 00:00:00",
        "data_cadastro": "2018-02-13 00:00:00",
        "usuario_cadastro": 1,
        "usuario_alteracao": "1",
        "status": 1
    }
}

Note that, it has an attribute of json called "farm" which is another table, but I can not do this way without being by find($id) of Eloquent would have another way of doing such a thing ?

  • I don’t understand what you want to do?

  • So, I wanted to make a query in the table Animals, which in turn has one to Many relation to the table Farms, I wanted to "merge" the queries to return a JSON/Array in the format given above.

  • Utilize with and specify the name of the relationship. !!! understood? Animal::with('fazenda')->get()

  • 1

    I understood yes, and by the way, I got it. I made such a big deal out of it and in the end it was just that. Thank you very much!

No answers

Browser other questions tagged

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