I'm trying to order the tests for each user in descending order of created_at. I tried it in the template but I didn't succeed. These are my tables:
| users | | courses | | tests |
| ---------- | |------------| |------------|
| id | | id | | id |
| name | | name | | name |
| created_at | | created_at | | created_at |
| user_id | | course_id |
A user has many courses and a course has many tests. I'll like to order all the tests in descending order of created_at.
I tried this in my template:
@foreach(User::find($user->id)->courses as $course)
@foreach(Course::find($course->id)->tests as $test)
<p>Name: {{$test->name}}</p>
<p>Date: {{$test->created_at}}</p>
@endforeach
@endforeach
Edit: There are my models
User.php
public function courses()
{
return $this->hasMany('Course');
}
Course.php
public function user()
{
return $this->belongsTo('User');
}
public function test()
{
return $this->hasMany('Test');
}
Test.php
public function courses()
{
return $this->belongsTo('Course');
}