1

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.

5 Answers 5

4

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
Sign up to request clarification or add additional context in comments.

Comments

0

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

4 Comments

That would iterate through each agent each time it iterates through one appt.
Caused an infinite loop.
@user3158900 exactly... I need it not to do this.
aaa sorry I miss understood then.
0

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.

3 Comments

Had a massive legacy database and unfortunately using the ORM wasn't an option. DB::get() is returning objects due to my settings. Since it's been doing this for so long I can't change it system wide I'd have to edit everything. I can just convert it to an array, right?
This answer cuts off after the last appt while there are still agents.
Is there a reason why you can't loop through appts and then loop through agents after? Why do they need to be done within the same loop?
0

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

5 Comments

@walterjones I updated my answer, you should convert stdClass to array. i.e. $appts->to_array() and $agents->to_array() before merging them
Call to a member function to_array() on a non-object
Sorry instead of to_array() use toArray()
Be aware that this solution has potential to overwrite some properties. For example if appt and agent both have a name property, the agent's name will override the appointment's name.
@user3158900 you're right. I edited my answer and added array_merge_recursive()
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.