Loop trough a collection

Posted 2 years ago by ekrist1

I´m trying to loop trough a collection, but i keep hitting the error: Undefined property: Illuminate\Database\Eloquent\Collection::$leaguestatistics (View: /Applications/MAMP/htdocs/sport/resources/views/frontend/seasontable.blade.php)

If i return the collection it has the right data:

[{"id":1,"created_at":"2016-09-18 00:00:00","updated_at":"2016-09-18 00:00:00","deleted_at":null,"leaguename":"Tippeligaen 2016","slug":"tippeligaen-2016","datafrom":"2015-09-18","datoto":"2016-09-18","sortkey":1,"description":"Norges eneste","leaguestatistics":[{"id":1,"created_at":"2016-09-18 00:00:00","updated_at":"2016-09-18 00:00:00","deleted_at":null,"wongames":10,"lostgames":10,"tiegames":10,"playedgames":10,"goalswon":10,"goalslost":8,"rank":1,"league_id":1,"team_id":1},{"id":2,"created_at":"2016-09-18 00:00:00","updated_at":"2016-09-18 00:00:00","deleted_at":null,"wongames":5,"lostgames":5,"tiegames":5,"playedgames":5,"goalswon":5,"goalslost":2,"rank":2,"league_id":1,"team_id":2}]}]

Any idees how to loop trough the secound array?

Model:

    public function leaguestatistics()
    {
        return $this->hasMany('App\Stats', 'league_id');
    }

}

Controller:

  public function show($slug)
   {
    $leaguestats = League::where('slug', $slug)->with('leaguestatistics')->get(); 
    //return $leaguestats;
    return view ('frontend.seasontable', compact('leaguestats'));
   }
}

View:

@foreach ($leaguestats as $leaguestat)
@foreach ($leaguestats->leaguestatistics as $teamestat) 
-->code
@endforeach 
@endforeach

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.