Support the ongoing development of Laravel.io →
Database Eloquent

Hello All,

I have a self project. I am trying to create an application that can track my financial status.

So I have an Account table and model. I also have a transactions table and model which is in one to many relationship with account table (One Account has many transactions)

What I want is displaying all accounts with last 3 transactions each. I couldn't figure out how to do it. Could you help me please?

Last updated 3 years ago.
0

You can't limit eager load for list.

Meybe something like this?

    Account::with(
        [
            'transactions' => function ($comment) {
                    $comment->where('created_at', '<=', '1999-12-21');
                }
        ]
    )->get();

Or you can perform standard query with join to get all (last 3 transaction ids) and inject to with scope

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Quanthir quanthir Joined 18 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.