laravel循环通过急切加载的嵌套关系

时间:2012-12-25 22:02:51

标签: php foreach nested-loops laravel

再一次努力,可以提供一些帮助 它可能看起来很有用,但不是,:))

我有一组渴望加载和关系的数组。这是代码(Laravel)

$user = User::with(array('profile','classrooms','warnings','observation','complaint','pmainduction','workshops','grievances','grievances.grievanceaction'))
    ->where('id', '=', $id)->get();

这可以在每个员工的个人资料页面中完美地获取数据。当我遍历申诉和每个申诉的申诉时,它会复制数据并且不会像在print_r中那样显示

数组的一部分是

[grievances] => Array
                    (
                        [0] => Grievance Object
                            (
                                [attributes] => Array
                                    (
                                        [id] => 1
                                        [user_id] => 8
                                        [company] => Company name
                                        [date] => 2012-12-24
                                        [nature] => Visa
                                        [details] => can't renew visa.                                                                                         [action] => emailed company
                                        [status] => 1
                                        [created_at] => 0000-00-00 00:00:00
                                        [updated_at] => 0000-00-00 00:00:00
                                    )

                                [original] => Array
                                    (
                                        [id] => 1
                                        [user_id] => 8
                                        [company] => Company name
                                        [date] => 2012-12-24
                                        [nature] => Visa
                                        [details] => can't renew visa.                                                                                  [action] => emailed company
                                        [status] => 1
                                        [created_at] => 0000-00-00 00:00:00
                                        [updated_at] => 0000-00-00 00:00:00
                                    )

                                [relationships] => Array
                                    (
                                        [grievanceaction] => Array
                                            (
                                                [0] => Grievanceaction Object
                                                    (
                                                        [attributes] => Array
                                                            (
                                                                [id] => 1
                                                                [action] => do something here                                                                                                                                [grievance_id] => 1
                                                                [created_at] => 2012-12-25 00:00:00
                                                                [updated_at] => 2012-12-25 20:03:04
                                                            )

                                                        [original] => Array
                                                            (
                                                                [id] => 1
                                                                [action] => do something here
                                                                [grievance_id] => 1
                                                                [created_at] => 2012-12-25 00:00:00
                                                                [updated_at] => 2012-12-25 20:03:04
                                                            )

                                                        [relationships] => Array
                                                            (
                                                            )

                                                        [exists] => 1
                                                        [includes] => Array
                                                            (
                                                            )

                                                    )

                                                [1] => Grievanceaction Object
                                                    (
                                                        [attributes] => Array
                                                            (
                                                                [id] => 2
                                                                [action] => some text here
                                                                [grievance_id] => 1
                                                                [created_at] => 2012-12-25 00:00:00
                                                                [updated_at] => 2012-12-25 00:00:00
                                                            )

                                                        [original] => Array
                                                            (
                                                                [id] => 2
                                                                [action] => some text here
                                                                [grievance_id] => 1
                                                                [created_at] => 2012-12-25 00:00:00
                                                                [updated_at] => 2012-12-25 00:00:00
                                                            )

                                                        [relationships] => Array
                                                            (
                                                            )

                                                        [exists] => 1
                                                        [includes] => Array
                                                            (
                                                            )

                                                    )

                                            )

                                    )

                                [exists] => 1
                                [includes] => Array
                                    (
                                    )

                            )

                        [1] => Grievance Object
                            (
                                [attributes] => Array
                                    (
                                        [id] => 5
                                        [user_id] => 8
                                        [company] => Company name
                                        [date] => 2012-12-25
                                        [nature] => Housing
                                        [details] => another issue here
                                        [action] => meet with company staff
                                        [status] => 1
                                        [created_at] => 2012-12-25 20:24:57
                                        [updated_at] => 2012-12-25 20:24:57
                                    )

                                [original] => Array
                                    (
                                        [id] => 5
                                        [user_id] => 8
                                        [company] => Company name
                                        [date] => 2012-12-25
                                        [nature] => Housing
                                        [details] => another issue here
                                        [action] => meet with company staff
                                        [status] => 1
                                        [created_at] => 2012-12-25 20:24:57
                                        [updated_at] => 2012-12-25 20:24:57
                                    )

                                [relationships] => Array
                                    (
                                        [grievanceaction] => Array
                                            (
                                                [0] => Grievanceaction Object
                                                    (
                                                        [attributes] => Array
                                                            (
                                                                [id] => 3
                                                                [action] => different text here
                                                                [grievance_id] => 5
                                                                [created_at] => 2012-12-25 00:00:00
                                                                [updated_at] => 2012-12-25 23:29:21
                                                            )

                                                        [original] => Array
                                                            (
                                                                [id] => 3
                                                                [action] => different text here
                                                                [grievance_id] => 5
                                                                [created_at] => 2012-12-25 00:00:00
                                                                [updated_at] => 2012-12-25 23:29:21
                                                            )

                                                        [relationships] => Array
                                                            (
                                                            )

                                                        [exists] => 1
                                                        [includes] => Array
                                                            (
                                                            )

                                                    )

                                            )

                                    )

你可以看到我有怨言对象1& 2他们每个人都有自己的不满。 在我的输出中,我想实现这个

--------------------------------------------------------------
|  Grievance name: 1  |   Date:     |   Action:    |  Status: |
--------------------------------------------------------------
|   (grievanceaction data for 1st grievance in loop )        |
|   action: relationship action here      date: with date    |
|                                                            |
--------------------------------------------------------------
--------------------------------------------------------------
|  Grievance name: 2  |   Date:     |   Action:    |  Status: |
--------------------------------------------------------------
|   (grievanceaction data for 2nd grievance in loop )        |
|   action: relationship action here      date: with date    |
|                                                            |
--------------------------------------------------------------

我目前有

--------------------------------------------------------------
|  Grievance name: 1  |   Date:     |   Action:    |  Status: |
--------------------------------------------------------------
|   (grievanceaction data for 1st grievance in loop )         |
|                                                             |
|                                                             |
--------------------------------------------------------------
--------------------------------------------------------------
|  Grievance name: 2  |   Date:     |   Action:    |  Status: |
--------------------------------------------------------------
|   (grievanceaction data for 1st grievance in loop )         |
|                                                             |
|                                                             |
--------------------------------------------------------------

代码是

<table width="100%" class="table table-striped table-bordered">

@if(empty($user['0']->grievances))
   <thead>
   <tr>
   <th colspan="3">  No Grievances logged.</th>
   </tr>
   </thead>
@else
   <thead>

@foreach ($user['0']->grievances as $grievance )

<tr>
<th>Date: {{ date("d-M-Y",strtotime($grievance->date)) }}</th>
<th>Nature: {{ $grievance->nature }}</th>
<th>Initial Action:</strong> {{ $grievance->action }}  </th>
<th>Status: 

    @if ( $grievance->status == 1 )
          Active
    @else
          Resolved
    @endif
</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="4">Description: {{ $grievance->details }} </td>
</tr>
<tr>
<td colspan="4">

              <!-- here is where i try and loop through the grievanceactions -->

              <table width="100%" class="table table-striped table-bordered">
              <tr>
              <th scope="col">Date</th>
              <th scope="col">Action</th>
              </tr>
              <tbody>
              @foreach ($user['0']->grievances['0']->grievanceaction as $grievance )
              <tr>
              <td>{{ date("D, d-M-Y H:i",strtotime($grievance->created_at)) }}</td>
              <td>{{ $grievance->action }}</td>
              </tr>
              @endforeach
              </tbody>
              </table>
</td>
</tr>
@endforeach

@endif
</td>
</tr>
</tbody>
</table>

我真的很感激一些帮助,谢谢

:)

1 个答案:

答案 0 :(得分:1)

问题是你硬编码,你总是采取$user['0']->grievances['0']的冤情,例如第一个。申诉循环应该是:

              @foreach ($grievance->grievanceaction as $grievanceaction )
              <tr>
              <td>{{ date("D, d-M-Y H:i",strtotime($grievanceaction->created_at)) }}</td>
              <td>{{ $grievanceaction->action }}</td>
              </tr>
              @endforeach

编辑:一些注释:

  1. Blade中有一个@forelse循环,其中@empty部分为get(),这样可以在顶部保存if。
  2. 您不需要为用户使用first(),您只需使用{{1}},因此每次都不需要['0']。
  3. 通常的做法是将* -many关系复数命名为复数,以表示它是一系列结果(grievanceaction - &gt; grievanceactions)。
相关问题