Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9069383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:24:53+00:00 2026-06-16T17:24:53+00:00

struggling once again and could do with some help. It may look looooong but

  • 0

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

🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T17:24:54+00:00Added an answer on June 16, 2026 at 5:24 pm

    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:

    1. There is a @forelse loop in Blade which has an @empty part, this saves an if at the top.
    2. You don’t need to use get() for the user, you can simply use first() so [‘0’] not needed every time.
    3. It is a common practice to name *-many relations plural to indicate it is an array of results (grievanceaction -> grievanceactions).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Once again I'm struggling to find my answer on Google, but I'm sure it
I need your help once again, guys i'm struggling with a css dropdown menu
Iam struggling with NHibernate and its lazyload. I have a structure which I simplified
Once again, I'm struggling to understand an undocumented behavior of the method GetFiles in
I'm (once again) struggling with the creation of precompiled headers in conjunction with gcc
Once again I am struggling with date formatting in Rails. I am trying to
once again, I'm struggling with porting a Firefox extension to Internet Explorer 7, 8
I am really struggling with using the SqlLite in my App; I have set
I have been struggling with the RestKit since 15 days now. Once I receive
I have been struggling with trying to create/save multiple instances at once in Grails,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.