Mikegk
6 months ago
1086
7
Eloquent

Getting last 25 records from ASC ordered list

Posted 6 months ago by Mikegk

Hi guys,

sounds pretty easy and can be done via Subqueries, but currently I do not find a way to get an eager loaded property beeing limited to 25 in ASC order.

	//My try
	$model = SomeModel::with(['someProperties'=>function($q){
		$q->limit(25)
		    ->orderBy('created_at','ASC');
		/*
		* How to perform a subquery here - or how to take the last 25 Entries :/ ?!
		*/
	}])->get()
			

I just found a way to inverse sort the query with

SELECT * 
FROM (SELECT * FROM comments
      WHERE postID='$id' 
        AND state='0' 
      ORDER BY id DESC 
      LIMIT 3) t
ORDER BY id ASC;

...but I don't know how to implement that using Eloquent. Is there a more elegant way?

Please sign in or create an account to participate in this conversation.