Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

638
Views
Laravel: Obtenga el elemento de la matriz para mostrar en la tabla

¿Cómo obtener los datos del elemento de la matriz? A continuación se muestran los datos de mi matriz del objeto Tour .

Es posible que algunos recorridos no tengan asistentes al recorrido (matriz vacía).

Y habrá SOLO DOS registros (asistentes) para UN 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 ] }

Quiero extraer los datos y ponerlos dentro de la tabla como se muestra a continuación.

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

lima hoja

 @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 answers
Answer question

0

Puede envolver su matriz en una colección y tomar solo dos registros como este:

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

over 4 years ago · Santiago Trujillo Report

0

Puede usar forelse y break en 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 Report

0

Según tengo entendido, desea establecer datos para los asistentes a la gira y solo habrá dos asistentes por gira para que pueda mapear sus datos de acuerdo con su tabla de esta manera

 $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;

devolverá algo como esto

 [ { "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": "" } } ]

¡Con suerte, te ayudará con el resultado que deseas!

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!