Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

653
Visualizações
Laravel: Get element of array to display on table

How to get the element data of array? Below is my array data of object Tour.

Some Tour might not have tour_attendees (empty array).

And there will be ONLY TWO records (attendees) for ONE TOUR.

#items: array:3 [▼
      0 => {#2115 ▶}
      1 => {#3067 ▶}
      2 => {#2125 ▼
        +"id": 12
        +"schedule_date": "28-11-2021"
        +"created_at": "2021-11-18T00:16:42.000000Z"
        +"updated_at": "2022-01-19T08:29:05.000000Z"
        +"deleted_at": null
        +"tour_attendees": array:2 [▼           // array data that I want to extract 
          0 => {#3077 ▶}                       // attendee #1
          1 => {#3054 ▶}                       // attendee #2
        ]
      }

I want to extract the data and put inside the table as below.

| No  |    Attendee #1 |     Mobile      |    Attendee #2   |     Mobile      |
|     |                |  (Attendee #1)  |                  |  (Attendee #2)  |
|-----|----------------|-----------------|------------------|-----------------|
|  1  |                |                 |                  |                 |
|  2  |                |                 |                  |                 |

Blade file

@foreach ($tour->tour_attendees as $key => $value)
 <td>
    @if (!empty($tour->tour_attendees))
       @foreach ($tour->tour_attendees as $key => $attendee)
          @if ($key == 0)
             {{ $attendee->name }}
          @endif
       @endforeach
    @endif
  </td>
  <td> attendee #1 mobile</td>
@endforeach
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You can wrap your array in collection and take only two records like this:

collect($your_array)->take(2)->get()

over 4 years ago · Santiago Trujillo Relatório

0

You can use forelse and break on key === 2

@forelse ($tour->tour_attendees as $key => $attendee)
  @if ($key === 2)
    @break
  @endif
  <td>
     {{ $attendee->name }}
  </td>
  <td> 
     {{ $attendee->mobile }}
  </td>
@empty
  <td> - </td><td> - </td><td> - </td><td> - </td>
@endforelse

@if (count($tour->tour_attendees) === 1)
  <td> - </td><td> - </td>
@endif
over 4 years ago · Santiago Trujillo Relatório

0

As per my understanding you want to set data for tour attendees and there will be only two attendee per tour so you can map you data according to your table like this

$tours = collect([
    [
        "id" => 12,
        "schedule_date" => "28-11-2021",
        "created_at" => "2021-11-18T00:16:42.000000Z",
        "updated_at" => "2022-01-19T08:29:05.000000Z",
        "deleted_at" => null,
        "tour_attendees" => collect([
            [
                'id' => 1,
                "name" => "john doe",
                "phone" => "123456789"
            ], [
                'id' => 1,
                "name" => "john doe",
                "phone" => "123456789"
            ]
        ])
    ], [
        "id" => 12,
        "schedule_date" => "28-11-2021",
        "created_at" => "2021-11-18T00:16:42.000000Z",
        "updated_at" => "2022-01-19T08:29:05.000000Z",
        "deleted_at" => null,
        "tour_attendees" => collect([])
    ]
]);

$resposne = $tours->map(function ($tour) {

        return [
            "id" => $tour["id"],
            "schedule_date" => $tour["schedule_date"],
            'tour_attendess' => [
                'attendee_one_name' => $tour['tour_attendees'][0]['name'] ?? '',
                'attendee_one_phone' => $tour['tour_attendees'][0]['phone'] ?? '',
                'attendee_two_name' => $tour['tour_attendees'][1]['name'] ?? '',
                'attendee_two_phone' => $tour['tour_attendees'][1]['phone'] ?? '',
            ]
        ];
});

return $resposne;

it will return something like this

     [
{
"id": 12,
"schedule_date": "28-11-2021",
"tour_attendess": {
"attendee_one_name": "john doe",
"attendee_one_phone": "123456789",
"attendee_two_name": "john doe",
"attendee_two_phone": "123456789"
}
},
{
"id": 13,
"schedule_date": "28-12-2021",
"tour_attendess": {
"attendee_one_name": "",
"attendee_one_phone": "",
"attendee_two_name": "",
"attendee_two_phone": ""
}
}
]

Hopefully, it will help you with the result you want!

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda