php - Laravel Eloquent - equivalent to first() for last? -
i have few models such user, post, comment, etc.
in user, have:
public function posts() { return $this->hasmany('post'); } i can first post via $customer->posts()->first(), if want latest post? there no last() can see.
this further compounded hasmanythrough relationship (unfortunately inherited wacky schema):
public function comments() { return $this->hasmanythrough('comment', 'post'); } if try $this->comments()->orderby('comments.id', 'desc')->first(); returns user object??
no, this
// user model $this->comments()->orderby('comments.id', 'desc')->first(); won't return user model.
and asked for, this:
$customer->posts()->latest()->first();
Comments
Post a Comment