2012-12-25 63 views
0

掙扎再次,可以做一些幫助。
它可能看起來looooong,但它不是,:)laravel循環通過渴望加載嵌套關係

我有一組數組是渴望加載和關係。這裏的代碼(Laravel)

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

這將獲得數據完美的每個員工在他們的個人資料頁時。當i循環通過的不滿和每個申訴它重複的數據,並像在的print_r不顯示它的grievanceaction

陣列的一部分是

[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> 

我真的很感激一些幫助,感謝

:)

+0

我希望你的代碼是在僞代碼中? – Jelmer

+0

爲什麼?這是根據文件。它是laravel模板引擎格式。唯一應該出錯的代碼可能是我的循環($ user ['0'] - > grievances ['0'] - > grievanceaction爲$ grievance)。請解釋你的評論? [鏈接](http://laravel.com/docs/database/eloquent)和[鏈接](http://laravel.com/docs/views/templating#blade-control-structures) – user1740151

+0

因爲它不是原生的PHP。你的問題沒有任何地方是'laravel'。在您的評論之後,我對它進行了搜索,並且我只能在標題和標籤中找到該信息。所以請更新你的問題,並明確它是關於laravel。 – Jelmer

回答

1

問題是你硬編碼,你總是採取$user['0']->grievances['0'],如第一個抱怨。該grievanceaction循環應該是:

   @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. 有一個@forelse環路葉片,其具有@empty部分,這樣可以節省的,如果在頂部。
  2. 您不需要爲用戶使用get(),您可以簡單地使用first(),因此每次都不需要['0']。
  3. 這是一種常見的做法,命名* -many關係複數來表明它是一組結果(grievanceaction - > grievanceactions)。
+0

非常感謝,工作完美,解釋和提示非常有幫助。我做了改變,並得到了一切工作,除了我改變 - > get()to - >第一,但如果我刪除['0']我得到的錯誤。我現在可以在哪裏刪除['0']?謝謝 – user1740151

+0

'first()'應該返回一個模型(User在這裏),而不是一個模型數組,因此你不需要循環用戶和/或使用數組訪問器。 – TLGreg

+0

謝謝,我終於搞定了,明白現在發生了什麼。非常感激。你有沒有機會知道[this](http://stackoverflow.com/questions/14040495/laravel-insert-loop-with-many-to-many-relationship)? :) – user1740151