DB: Load list from one table with total count of relations and with only the last relation


let’s say, I have a two tables: “Orders” and “OrderItems”…

Now I want to load a list of "Orders" and with that a count of all "OrderItems" for each "Order".

And also for each "Order" only the last "OrderItem".

I know there are simple ways to handle this, but what is the fastest way with a minimum of queries against the DB.

I know, that I can “Preload” using the “with” function, but I don’t get this to work right :(

Thanks for your help,


At the end of the page in the guide is exactly what you want to do.