Laravel foreach 有多个对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25490844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:59:42  来源:igfitidea点击:

Laravel foreach with multiple objects?

phplaravelforeach

提问by walter jones

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

我有一个从 Laravel 的 DB 类返回的对象,我传递给我的视图:

    @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.

除了我有另一个需要迭代的对象,但是如果我将它包含在当前的 foreach 下,它显然会迭代多次。

Is there anyway to do something like:

反正有没有做这样的事情:

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

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

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

以上?像这样的多个foreach?或者有什么方法可以达到这个效果?

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

或者某种方式将它们合并在一起?它们都是 stdClass。

回答by Jo?o Henrique M. Pellissari

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 放在<td>我需要的元素中。像这样的东西。

@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

回答by WillyBurb

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

回答by user1669496

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.

DB::get() 应该返回一个数组,因此您应该可以轻松使用 for 循环。它不会那么漂亮,这是假设您以匹配的顺序返回它们,并且每个应用程序都有一个代理。

        @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.

我不确定你是如何匹配它们的,但我建议在 Eloquent 中建立一个实际的关系。工作起来会容易得多。

回答by user1669496

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

作为解决方案,首先,您可以使用array_merge()函数合并两个数组:

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

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

注意:array要从检索stdClass,您应该使用toArray()方法

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

重要提示:如果您在数组中有相同的键并且想要同时拥有这两个键,则应使用array_merge_recursive()而不是array_merge().

Then, you can use @foreach:

然后,您可以使用@foreach

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

回答by DevBodin

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