0

I am trying to read a nested array and show the value in a blade table.

Here is the data I see in view:

{
  "sub_issue_id": 2,
  "sub_issue": "Publick speaking",
  "sub_issue_desc": "This sub issue tracks the progress related to public speaking. ",
  "sub_issue_status": 1,
  "sub_issue_main_issue_id": 1,
  "sub_issue_user_id": 1,
  "created_at": "2015-01-18 13:45:34",
  "updated_at": "2015-01-18 13:45:34",
  "main_issue": [
    {
      "main_issue_id": 1,
      "main_issue": "Commuincations",
      "main_issue_desc": "This main task tracks progress on communications. ",
      "main_issue_status": 1,
      "main_issue_user_id": 1,
      "created_at": "2015-01-18 00:20:29",
      "updated_at": "2015-01-18 00:20:29"
    }
  ]
}
{
  "sub_issue_id": 3,
  "sub_issue": "Emails",
  "sub_issue_desc": "This subissue tracks the progress related to emails. ",
  "sub_issue_status": 1,
  "sub_issue_main_issue_id": 1,
  "sub_issue_user_id": 1,
  "created_at": "2015-01-18 13:51:48",
  "updated_at": "2015-01-18 13:51:48",
  "main_issue": [
    {
      "main_issue_id": 1,
      "main_issue": "Commuincations",
      "main_issue_desc": "This main task tracks progress on communications. ",
      "main_issue_status": 1,
      "main_issue_user_id": 1,
      "created_at": "2015-01-18 00:20:29",
      "updated_at": "2015-01-18 00:20:29"
    }
  ]
}

Basically I need to read the array "main_issue" and show the value for "main_issue" attribute (in the above data "Communications")

Here are the options I tried:

@foreach( $subIssues as $subIssue )
            {{ $subIssue }}
            <tr>
                {{--<td>{{ $subIssue->main_issue[0]->main_issue }}</td>--}}
                {{--<td>{{ $subIssue->main_issue->first()->main_issue }} </td>--}}
                {{--<td>{{ $subIssue->main_issue->firstRow->get(0)->main_issue }} </td>--}}

Here are the model & controller code..public function SubIssue()
    {
        return $this->belongsTo('App\Models\Issues\SubIssue', 'main_issue_id','sub_issue_main_issue_id'); /**
     * Relation between sub issue and main issue
     */
    public function MainIssue()
    {
        return $this->hasMany('App\Models\Issues\MainIssue', 'main_issue_id', 'sub_issue_main_issue_id');
    }
$mainIssues = $mainIssue->where('main_issue_user_id', $id)->get();
        //dd($mainIssues->toArray());
        return view('issues.mainIssues.mainIssues', compact('mainIssues'));
    }enter code here
5
  • What format is your data coming through as? An Eloquent model? An Array? Can you post your model and controller code? Commented Jan 19, 2015 at 18:33
  • It looks like you have JSON being sent through for some reason, as it happens Eloquent models and some other classes have a toJson method, is this being called by accident? Where is the data coming from and what is it's expected state? Commented Jan 19, 2015 at 19:55
  • Yes.. It's coming as an eloquent model.. I am using compact function to send the data.. Commented Jan 19, 2015 at 20:09
  • Here are the model & controller code..public function SubIssue() { return $this->belongsTo('App\Models\Issues\SubIssue', 'main_issue_id','sub_issue_main_issue_id'); /** * Relation between sub issue and main issue */ public function MainIssue() { return $this->hasMany('App\Models\Issues\MainIssue', 'main_issue_id', 'sub_issue_main_issue_id'); } $mainIssues = $mainIssue->where('main_issue_user_id', $id)->get(); //dd($mainIssues->toArray()); return view('issues.mainIssues.mainIssues', compact('mainIssues')); } Commented Jan 19, 2015 at 20:11
  • Where is the $subIssues variable in the view coming from? In your code sample, it looks like you only pass a collection of MainIssues to the view. Commented Jan 20, 2015 at 3:10

1 Answer 1

1

Try the following:

{{ $subIssue->MainIssue->main_issue }}

Consider using camelCase for method names, which can make the code easier to read. MainIssue in the line above is the MainIssue() function of the SubIssue class which should really be mainIssue() if you follow PSR-1 as Laravel does: http://www.php-fig.org/psr/psr-1/

One more tip (from personal experience): don't repeat the table name in all of the field names. $main_issue->name ($main_issue->status, ect) is easier to read than main_issue->main_issue.

Edit: It looks like your keys are reversed in your relationship methods. The Laravel docs show that the second parameter is the foreign key, and the third parameter as the local key. (though the terms appear to be mixed up in one example in the One to One section) Try the following-

//In SubIssue model
mainIssue()
{
    return $this->belongsTo('App\Models\Issues\MainIssue', 'sub_issue_main_issue_id', 'main_issue_id'); 
}

//In MainIssue model
public function subIssue()
{
    return $this->hasMany('App\Models\Issues\SubIssue', 'sub_issue_main_issue_id', 'main_issue_id');
}

I find keys like 'sub_issue_main_issue_id' hard to follow, and if you use keys named 'id' for your primary keys, and 'table_name_id' (such as main_issue_id) for your foreign keys, you can take advantage of Laravel's conventions (you won't have to specify what keys to use in relationships).

Also note: If you use your own primary keys, make sure to set the $primaryKey variable in your model:

protected $primaryKey = 'main_issue_id';
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your time Dave. I tried your suggestion it didn't work. I will change method name to camel case. The reason for table name names in columns is, I have same column name in multiple tables so to differenciate I did this approach.
I also have lots of columns with the same names (in different tables) but I still find it a lot easier to not include the table name in the column. Actually, I suspect that the issue is with your definitions of the foreign and local keys in your relationships. I will read through them again and edit my answer.
This worked for me. I had {{$log->project->name}} , where name was inside array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.