mercredi 13 mars 2019

How to get an array from a single field from related model in laravel?

The following:

Customer::with('tickets:customer_id,subject')->get();

returns the following:

[
  {
    "id": 1,
    "company": "Test Ltd",
    "support": "Standard",
    "tickets": [
      {
        "customer_id": "1",
        "name": "Test ticket 1"
      },
      {
        "customer_id": "1",
        "name": "Test ticket 2"
      }
    ]
  }
]

How can I flatten the related field, tickets to get tickets: ["Test ticket 1", "Test ticket 2"]? I tried doing a ->flatten() after ->get() but that didn't work at all. It feels like it should be something simple that I'm missing and just can't find in the docs or by googling (thanks for the word with, that screws pretty much all sane results...)



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire