Reputation: 155
struggling once again and could do with some help.
It may look looooong but it's not, :)
I have a set of arrays which are eager loaded and relational. here's the code (Laravel)
$user = User::with(array('profile','classrooms','warnings','observation','complaint','pmainduction','workshops','grievances','grievances.grievanceaction'))
->where('id', '=', $id)->get();
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
Part of the array is
[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
(
)
)
)
)
As you can see i have grievance objects 1 & 2 and each of them has its own grievanceaction. In my output i would like to achieve this
--------------------------------------------------------------
| 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 |
| |
--------------------------------------------------------------
I currently have
--------------------------------------------------------------
| 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 is
<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>
I would really appreciate some help, thanks
:)
Upvotes: 0
Views: 3854
Reputation: 8449
The problem is you hardcoded that you always take the grievanceactions from the $user['0']->grievances['0']
eg the first one. The grievanceaction loop should be:
@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
Edit: A few notes:
@forelse
loop in Blade which has an @empty
part, this saves an if at the top.get()
for the user, you can simply use first()
so ['0'] not needed every time.Upvotes: 1