Note that there are some explanatory texts on larger screens.

plurals
  1. POlaravel loop through eager loaded nested relationships
    primarykey
    data
    text
    <p>struggling once again and could do with some help.<br> It may look looooong but it's not, :)</p> <p>I have a set of arrays which are eager loaded and relational. here's the code (Laravel)</p> <pre><code>$user = User::with(array('profile','classrooms','warnings','observation','complaint','pmainduction','workshops','grievances','grievances.grievanceaction')) -&gt;where('id', '=', $id)-&gt;get(); </code></pre> <p>This gets the data perfectly for each member of staff when in their profile page. When i loop through grievances and the grievanceaction for each grievance it duplicates the data and doesn't display it like in the print_r</p> <p>Part of the array is</p> <pre><code>[grievances] =&gt; Array ( [0] =&gt; Grievance Object ( [attributes] =&gt; Array ( [id] =&gt; 1 [user_id] =&gt; 8 [company] =&gt; Company name [date] =&gt; 2012-12-24 [nature] =&gt; Visa [details] =&gt; can't renew visa. [action] =&gt; emailed company [status] =&gt; 1 [created_at] =&gt; 0000-00-00 00:00:00 [updated_at] =&gt; 0000-00-00 00:00:00 ) [original] =&gt; Array ( [id] =&gt; 1 [user_id] =&gt; 8 [company] =&gt; Company name [date] =&gt; 2012-12-24 [nature] =&gt; Visa [details] =&gt; can't renew visa. [action] =&gt; emailed company [status] =&gt; 1 [created_at] =&gt; 0000-00-00 00:00:00 [updated_at] =&gt; 0000-00-00 00:00:00 ) [relationships] =&gt; Array ( [grievanceaction] =&gt; Array ( [0] =&gt; Grievanceaction Object ( [attributes] =&gt; Array ( [id] =&gt; 1 [action] =&gt; do something here [grievance_id] =&gt; 1 [created_at] =&gt; 2012-12-25 00:00:00 [updated_at] =&gt; 2012-12-25 20:03:04 ) [original] =&gt; Array ( [id] =&gt; 1 [action] =&gt; do something here [grievance_id] =&gt; 1 [created_at] =&gt; 2012-12-25 00:00:00 [updated_at] =&gt; 2012-12-25 20:03:04 ) [relationships] =&gt; Array ( ) [exists] =&gt; 1 [includes] =&gt; Array ( ) ) [1] =&gt; Grievanceaction Object ( [attributes] =&gt; Array ( [id] =&gt; 2 [action] =&gt; some text here [grievance_id] =&gt; 1 [created_at] =&gt; 2012-12-25 00:00:00 [updated_at] =&gt; 2012-12-25 00:00:00 ) [original] =&gt; Array ( [id] =&gt; 2 [action] =&gt; some text here [grievance_id] =&gt; 1 [created_at] =&gt; 2012-12-25 00:00:00 [updated_at] =&gt; 2012-12-25 00:00:00 ) [relationships] =&gt; Array ( ) [exists] =&gt; 1 [includes] =&gt; Array ( ) ) ) ) [exists] =&gt; 1 [includes] =&gt; Array ( ) ) [1] =&gt; Grievance Object ( [attributes] =&gt; Array ( [id] =&gt; 5 [user_id] =&gt; 8 [company] =&gt; Company name [date] =&gt; 2012-12-25 [nature] =&gt; Housing [details] =&gt; another issue here [action] =&gt; meet with company staff [status] =&gt; 1 [created_at] =&gt; 2012-12-25 20:24:57 [updated_at] =&gt; 2012-12-25 20:24:57 ) [original] =&gt; Array ( [id] =&gt; 5 [user_id] =&gt; 8 [company] =&gt; Company name [date] =&gt; 2012-12-25 [nature] =&gt; Housing [details] =&gt; another issue here [action] =&gt; meet with company staff [status] =&gt; 1 [created_at] =&gt; 2012-12-25 20:24:57 [updated_at] =&gt; 2012-12-25 20:24:57 ) [relationships] =&gt; Array ( [grievanceaction] =&gt; Array ( [0] =&gt; Grievanceaction Object ( [attributes] =&gt; Array ( [id] =&gt; 3 [action] =&gt; different text here [grievance_id] =&gt; 5 [created_at] =&gt; 2012-12-25 00:00:00 [updated_at] =&gt; 2012-12-25 23:29:21 ) [original] =&gt; Array ( [id] =&gt; 3 [action] =&gt; different text here [grievance_id] =&gt; 5 [created_at] =&gt; 2012-12-25 00:00:00 [updated_at] =&gt; 2012-12-25 23:29:21 ) [relationships] =&gt; Array ( ) [exists] =&gt; 1 [includes] =&gt; Array ( ) ) ) ) </code></pre> <p>As you can see i have grievance objects 1 &amp; 2 and each of them has its own grievanceaction. In my output i would like to achieve this </p> <pre><code>-------------------------------------------------------------- | 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 | | | -------------------------------------------------------------- </code></pre> <p>I currently have </p> <pre><code>-------------------------------------------------------------- | 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 ) | | | | | -------------------------------------------------------------- </code></pre> <p>code is </p> <pre><code>&lt;table width="100%" class="table table-striped table-bordered"&gt; @if(empty($user['0']-&gt;grievances)) &lt;thead&gt; &lt;tr&gt; &lt;th colspan="3"&gt; No Grievances logged.&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; @else &lt;thead&gt; @foreach ($user['0']-&gt;grievances as $grievance ) &lt;tr&gt; &lt;th&gt;Date: {{ date("d-M-Y",strtotime($grievance-&gt;date)) }}&lt;/th&gt; &lt;th&gt;Nature: {{ $grievance-&gt;nature }}&lt;/th&gt; &lt;th&gt;Initial Action:&lt;/strong&gt; {{ $grievance-&gt;action }} &lt;/th&gt; &lt;th&gt;Status: @if ( $grievance-&gt;status == 1 ) Active @else Resolved @endif &lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td colspan="4"&gt;Description: {{ $grievance-&gt;details }} &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td colspan="4"&gt; &lt;!-- here is where i try and loop through the grievanceactions --&gt; &lt;table width="100%" class="table table-striped table-bordered"&gt; &lt;tr&gt; &lt;th scope="col"&gt;Date&lt;/th&gt; &lt;th scope="col"&gt;Action&lt;/th&gt; &lt;/tr&gt; &lt;tbody&gt; @foreach ($user['0']-&gt;grievances['0']-&gt;grievanceaction as $grievance ) &lt;tr&gt; &lt;td&gt;{{ date("D, d-M-Y H:i",strtotime($grievance-&gt;created_at)) }}&lt;/td&gt; &lt;td&gt;{{ $grievance-&gt;action }}&lt;/td&gt; &lt;/tr&gt; @endforeach &lt;/tbody&gt; &lt;/table&gt; &lt;/td&gt; &lt;/tr&gt; @endforeach @endif &lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; </code></pre> <p>I would really appreciate some help, thanks</p> <p>:)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload