walter jones
walter jones

Reputation: 35

Laravel foreach with multiple objects?

I have an object returned from Laravel's DB class that I pass to my view:

    @foreach ($appts as $appt)
        <tr>
            <td> {{ $appt->carrier }} </td>
            <td> {{ $appt->carrier }} </td>
            <td> {{ $appt->name }} </td>
            <td> {{ Format::up($appt->lines_of_authority_c) }} </td>
            <td> {{ Format::under($appt->status_c) }} </td>
            <td> {{ Format::date($appt->effective_date_c) }} </td>
        </tr>
    @endforeach

Except I have another object I need to iterate over, but if I include it under the current foreach it obviously iterates multiple times.

Is there anyway to do something like:

@foreach($appts as $appt, $agents as $agent)

The above? Multiple foreaches like this? Or some way to achieve this effect?

Or some way to merge them together? They are both stdClass.

Upvotes: 1

Views: 11855

Answers (5)

DevBodin
DevBodin

Reputation: 211

This is an ancient one, and definitely not the latest version but the solution isn't so tricky. (Mind you, my solution assumes you have the same column names.)

In the controller:

$appts = [Query Here]
$agents = [Query Here]

$combo = [$appts, $agents]

Then,

@foreach($combo as $individualquery)
    @foreach($individualquery as $whatever)
       {{ $whatever->thing }}
    @endforeach
@endforeach

Upvotes: 0

This is a very old question but I had to do something similar and couldn't find anything on the internet.

I put the second @foreach inside the <td> element that I needed. Something like this.

@foreach($cars as $car)
            <tr>
                <td>{!! $car->number !!}</td>
                <td>{!! $car->color !!}</td>
                <td>{!! $car->time_of_rent!!}</td>
                <td>
                    @foreach($clients as $client)
                        @if($client->rented_car === $car->id)
                            {!! $client->name !!}
                        @endif
                    @endforeach
                </td>

            </tr>
    @endforeach

Upvotes: 4

user1922137
user1922137

Reputation:

As a solution, first, you can merge two arrays using array_merge() function:

<?php $result = array_merge($appts->toArray(), $agents->toArray()); ?>

Note: For retrieving array from stdClass, you should use toArray() method.

Important: If you have same keys in arrays and you want to have both, you should use array_merge_recursive() rather than array_merge().

Then, you can use @foreach:

@foreach ($result as $data)
    <tr>
        <td>{{ $data->property }}</td>
    </tr>
@endforeach

Upvotes: 0

user1669496
user1669496

Reputation: 33148

DB::get() should be returning an array so you should easily be able to use a for loop. It won't be as pretty and this is assuming that you are returning them both in an order where they match and each appt would have one agent.

        @for ($i=0; $i < count($appts); $i++)
            <tr>
                <td> {{ $appt[$i]['carrier'] }} </td>
                <td> {{ $appt[$i]['name'] }} </td>
                <td> {{ Format::up($appt[$i]['lines_of_authority_c']) }} </td>
                <td> {{ Format::under($appt[$i]['status_c']) }} </td>
                <td> {{ Format::date($appt[$i]['effective_date_c']) }} </td>
                <td> {{ $agent[$i]['name'] }} </td>
            </tr>   
        @endfor

I'm not sure how you are matching them, but I would recommend setting up an actual relationship in Eloquent though. It would be much easier to work with.

Upvotes: 0

St. Jan
St. Jan

Reputation: 201

you can nest them like so:

@foreach ($appts as $appt)
    @foreach ($agents as $agent)
       <tr>
           <td> {{ $appt->carrier }} </td>
           <td> {{ $appt->carrier }} </td>
            <td> {{ $appt->name }} </td>
            <td> {{ Format::up($appt->lines_of_authority_c) }} </td>
            <td> {{ Format::under($appt->status_c) }} </td>
            <td> {{ Format::date($appt->effective_date_c) }} </td>
        </tr>
    @endforeach
@endforeach

Upvotes: 0

Related Questions