0

再次挣扎​​,可以做一些帮助。
它可能看起来很长,但事实并非如此,:)

我有一组急切加载和相关的数组。这是代码(Laravel)

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

这样可以在每个员工的个人资料页面中完美地获取数据。当我遍历申诉和每个申诉的 grievanceaction 时,它会复制数据并且不会像在 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>

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

:)

4

1 回答 1

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部分,这在顶部保存了一个 if。
  2. 您不需要get()为用户使用,您可以简单地使用first()所以 ['0'] 不需要每次。
  3. 通常将 *-many 关系命名为复数表示它是一个结果数组(grievanceaction -> grievanceactions)。
于 2012-12-26T08:37:29.950 回答