0

I am trying to get all data i need in one variable

--Subject

---Chapters related to subject

----Topics related to chapter

And this is what i have done!

    $subjects = Subject::all();
    $chapters = Chapter::all();
    $topics = Topic::all();

    foreach ($subjects as &$subject) 
    {
        $i = 0;

        $subject->related_chapters = array();
        $chapters_reltn = array();

        foreach ($chapters as $chapter) 
        {
            if ($chapter->subject_id == $subject->id) 
            {
                $chapters_reltn[$i]['id'] = $chapter->id; 
                $chapters_reltn[$i]['name'] = $chapter->name; 

                $j = 0;
                foreach ($topics as $topic) 
                {
                    if ($topic->chapter_id == $chapter->id) 
                    {
                        $chapters_reltn[$i]['related_topics'][$j]['id'] = $topic->id;
                        $chapters_reltn[$i]['related_topics'][$j]['name'] = $topic->name;

                        $j++;   
                    }
                }

                $i++;
            }
        };

        $subject->related_chapters = $chapters_reltn;
    }

When i dd() in laravel, i see all the data arranged in structure i wanted. The problem comes when accessing a specific data like so,

    @foreach($subjects as $subject)
        {{ $subject->name }}
        {{ $subject->related_chapters[0]['name'] }}
    @endforeach

i get an error:

    Undefined offset: 0 

Is there a better way of structuring my array, and getting data correctly. Please help!

4
  • actually it is not an error per se. it is just a notice that you might have an error but your code will work if you deactivate all notices. BUT it is mostly recomended to act on notices... in this case you could do that: {{ $subject->related_chapters[0]['name'] or ''}} Commented Jan 16, 2015 at 14:59
  • {{ $subject->related_chapters[0]['name'] or ''}} , this shows only one data, how can i get all data return? Commented Jan 16, 2015 at 16:41
  • 1
    loop over your data like you did while creating your $subject objects. there is no difference... Commented Jan 16, 2015 at 16:45
  • How silly of me, thanks ! I have managed to get all the data just by looping through the array, thanks for the guidance. Some times i can be a pain . . . Commented Jan 16, 2015 at 17:36

1 Answer 1

1

Undefined offset is an notice that comes when you try to access an array which does not exist . Make sure that a value exists in that index or you can do something like this just before accessing the value

If (isset ( $subject->related_chapters[0]['name']))

Sign up to request clarification or add additional context in comments.

4 Comments

blade has a more elegant approach to that problem: {{$someVar or 'default value'}} this automatically does the isset test for $someVar and if that fails then it returns the default value. if you don't need a default value then just leave it empty and you are good
you do recognize that the question is specific to laravel right? If you know laravel you also came across blade (although it is not mandatory, you still come across it in the introductory parts of most tutorials)
forgot to mention: balde is the templating engine of laravel
Ah never read the whole question.. My mistake.. Well I have no idea about laravel as well ;) will read upon it as I have stumbled across it. Thank you

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.