I have a Posts
table and polymorphic relation of Comments
table.
What I tried (not sure if it is the right way to go) but I get a message that column last_activity does not exist
class Post extends Eloquent {
...
$appends = ['last_activity'];
public function getLastActivityAttribute()
{
if($this->has('comments', '>', 0))
{
$comment = $this->comments()->getQuery()->orderBy('created_at', 'desc')->first();
return $comment->created_at;
}
else
{
return '1970-01-01 00:00:00';
}
}
...
}
So basically:
I want posts ordered by the created_at field of the comment
with Eloquent. I saw this, but it looks like he sorts the comments instead of the posts.
Help?