掙扎再次,可以做一些幫助。
它可能看起來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>
我真的很感激一些幫助,感謝
:)
我希望你的代碼是在僞代碼中? – Jelmer
爲什麼?這是根據文件。它是laravel模板引擎格式。唯一應該出錯的代碼可能是我的循環($ user ['0'] - > grievances ['0'] - > grievanceaction爲$ grievance)。請解釋你的評論? [鏈接](http://laravel.com/docs/database/eloquent)和[鏈接](http://laravel.com/docs/views/templating#blade-control-structures) – user1740151
因爲它不是原生的PHP。你的問題沒有任何地方是'laravel'。在您的評論之後,我對它進行了搜索,並且我只能在標題和標籤中找到該信息。所以請更新你的問題,並明確它是關於laravel。 – Jelmer