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

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -