0

i have a document with the following structure

db.projects.find().pretty()
{
    "_id" : ObjectId("587eb04ec5d281280d9244ed"),
    "root" : "Main Boards",
    "access" : "Public",
    "folders" : [
        {
            "foldersname" : "PROJECTS"
        }
    ]
}
{
    "_id" : ObjectId("587eb052c5d281280d9244ee"),
    "root" : "Shared Boards",
    "access" : "Public"
}

I successfully loaded the data in a variable. i can display the field 'root' contents in the blade view but i cant display the field 'foldersname'. here is my code below.

@foreach($structures as $structure)    
<a class="mdl-navigation__link" href="">{{$structure->root}}</a>

          @foreach($structure as $folders)

            <a class="mdl-navigation__link" href="">{{$folders->foldersname}}</a>
          @endforeach
@endforeach

I get the following error. i have been looking for solutions since 2 days. i have applied solutions from laravel forum and here but in vain. Can someone guide me to the right solution. thanks

Trying to get property of non-object

11
  • for the second foreach Try this @foreach($structure->folders as $folder)<a class="mdl-navigation__link" href="">{{$folder->foldersname}}</a>@endforeach Commented Jan 18, 2017 at 7:48
  • @Amani it does not work. still getting the above error. Commented Jan 18, 2017 at 7:53
  • Try this : @foreach($structure->folders as $folder) @if (isset($folder->foldersname) ) <a class="mdl-navigation__link" href="">{{$folder->foldersname}}</a>@endforeach Commented Jan 18, 2017 at 7:58
  • @Amani i now get the following error Invalid argument supplied for foreach() Commented Jan 18, 2017 at 8:04
  • can you return your object (in the Controlleur) instead of returning the view and post it in your question? Commented Jan 18, 2017 at 8:06

1 Answer 1

1

Try this :

 @foreach($structures as $structure)    
    <a class="mdl-navigation__link" href="">{{$structure->root}}</a>
    @if (isset($structure->folders))
       @foreach($structure->folders as $folder)
          @if(isset($folder['foldersname']) )      
             <a class="mdl-navigation__link" href="">{{$folder['foldersname']}} </a>
          @endif
       @endforeach
    @endif
 @endforeach
Sign up to request clarification or add additional context in comments.

17 Comments

{{$structure->root}} gets loaded but not {{$folder->foldersname}}
can you do a small test, just before you first foreach loop add this {{ $structures[0]->folders[0]->foldersname }} and see If you have some output.
stil getting this error Trying to get property of non-object
have you any function in your Project model called folders or foldersname ?
i do not have any function that call folders in the Project Model
|

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.