php - laravel 4: order object in template -
i'm trying order tests each user in descending order of created_at. tried in template didn't succeed. these 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 , course has many tests. i'll order tests in descending order of created_at.
i tried in template:
@foreach(user::find($user->id)->courses $course) @foreach(course::find($course->id)->tests $test) <p>name: {{$test->name}}</p> <p>date: {{$test->created_at}}</p> @endforeach @endforeach
edit: there 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'); }
you may try this, in controller
:
$users = user::with(array('courses.tests' => function($query) { $query->orderby('tests.created_at', 'desc'); }))->find($user->id);
then load view , pass $user
object this:
return view::make('your_view_name')->withuser($user);
then in view
try this:
@foreach($user->courses->tests $test) <p>name: {{ $test->name }}</p> <p>date: {{ $test->created_at }}</p> @endforeach
Comments
Post a Comment