Search code examples
phplaravel-5nested-loopslaravel-blade

Laravel Nested Loop in blade Templating


I am building a futsal league website where fixtures and results are to be displayed. The Result Model is like this

class Result extends Model {

//
protected $fillable = ['team_1', 'team_2', 'goals_1', 'goals_2', 'date', 'mom'];

}

Is there a way that I can loop through this data in blade and group by date?

Thanks in advance


Solution

  • In your controller, you can do as follows:

    return view('yourView', [
        'variable' => Result::groupBy('date')->get()
    ]);
    

    Read more about using models here

    Read more about using QueryBuilder here

    In your view, do as follows:

    @foreach($variable as $row)
        {{$row->date}}
    @endforeach
    

    Read more about blades here