Order By before limit/take.
$chatMessage = ChatMessage::with('user.role')
->orderBy('created_at', 'asc')
->take(10)
->get();
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Help please? I've created a live chat using vue2, how to get the last 10 rows of a table?
please see my query, thanks!
$chatMessage = ChatMessage::with('user.role')
->limit(10)
->get();
return $chatMessage;
Order By before limit/take.
$chatMessage = ChatMessage::with('user.role')
->orderBy('created_at', 'asc')
->take(10)
->get();
$chatMessage = ChatMessage::with('user.role')
->oldest()
->limit(10)
->get();
Produces exactly the same result as @willvincent but takes into account you tried latest() which is just ORDER BY created_at DESC whereas oldest() will give you ORDER BY created_at ASC and that take() is just an alias of limit().
If you give latest() or oldest() a date column other than created_at, it will order by that column instead.
$chatMessage = ChatMessage::with('user.role')
->oldest('updated_at')
->limit(10)
->toSql();
All that said, I actually think using orderBy() is less confusing but maybe that's me coming from writing tons of SQL before using any ORM's.
If you want to see the SQL produced by the query builder you can always use the toSql() method:
$chatMessage = ChatMessage::with('user.role')
->oldest()
->limit(10)
->toSql();
dd($chatMessage);
to get the query if that's helpful to you when debugging a query.
I agree.. I despite the existence of oldest and latest I personally find orderby to be easier to read as it's more explicit about what is actually happening.. less hidden "magic" literally all it does is add the very same orderBy clause. :)
thanks for the reply,
just remove oldest or orderBy created asc, just the same results as this
$chatMessage = ChatMessage::with('user.role')
->take(10)
->get();
I want to achieve is, if I have 10 rows of messages (take/limit 10)
message 1
message 2
message 3
message 4
message 5
message 6
message 7
message 8
message 9
message 10
then if a new message is added
message 11
if I refresh the page, I want to display is
message 2
message 3
message 4
message 5
message 6
message 7
message 8
message 9
message 10
message 11
up
please? anyone?
Dude, you aren't making any sense.
message 1 // i'm brand new. full of hope and optimism
. . .
message 10 // i'm a geezer 'bout to join the choir eternal
New bro comes and adds a message
message 1 // yo, i'm the newest bro, bro.
message 2 // i was #1. fml
. . .
message 10 // i'm old and busted
What you are requesting would be to chop off the newest messages and that seems pretty pointless.
I suppose there is one rational explanation...you want to reverse the order in which you are looping over the messages for display. In that case:
return $chatMessage->reverse();
Such that you'd have:
message 10 // oldest message - goodbye cruel world
. . .
message 1 // wee, i'm poppin' fresh, yo
Along comes a new message...
message 10 // i was 9, but now i'm 10
. . .
message 2 // hey, i used to be #1
message 1 // 'sup, i'm new here. a/s/l?
Am I even close? At any rate, let me know if I'm way off base.
Also, start a new thread next time instead of completely updating your original. Your update totally contradicts what you asked and makes the replies we left seem crazy.
@lindstrom thanks for your reply sir,
yes I'd tried reverse method,
public function getChatMessage(Game $game)
{
$chatMessage = ChatMessage::with('user.role')
->where('game_id', $game->id)
->latest()
->take(5)
->get();
$messages = $chatMessage->reverse();
return $messages->all();
}
but vuejs doesn't render it in reverse
mounted() {
this.getChatMessage();
},
methods: {
getChatMessage() {
this.$http.get('/api/message/' + this.game.id)
.then(response => {
this.posts = response.data;
});
},
addNewMessage() {
this.btnChat = true
this.$http.post('/api/message/' + this.game.id, {message: this.newMessage})
.then((response) => {
this.newMessage = '';
this.btnChat = false
});
},
}
also tried using computed property
computed: {
reversedMessage: function() {
return this.posts.reverse();
},
},
<li v-for="post in reversedMessage">
<div class="chat-info">
<span v-show="post.user.role.label == 'General Admin'" class="name generaladmin pull-left">{{ post.user.username ? post.user.username : post.user.email }}: </span>
<span v-show="post.user.role.label == 'Administrator'" class="name admin pull-left">{{ post.user.username }}: </span>
<span v-show="post.user.role.label == 'Customer Service Representative'" class="name csr pull-left">{{ post.user.username }}: </span>
<span v-show="post.user.role.label == 'Member'" class="name member pull-left">{{ post.user.username ? post.user.username : post.user.email }}: </span>
<div class="message">
{{ post.message }}
</div>
</div>
</li>
If you reverse it, it doesnt change the keys. Javascript uses the keys to order it so this won't work. Try running values() on it too:
$messages = $chatMessage->reverse()->values();
@zachleigh it work! thanks a lot sir!
Stupid Javascript. Thanks @zachleigh for the enlightenment.
Better way with eloquent
Model::latest()->take(10)->get();
Please or to participate in this conversation.