Fetching just one row from a relation

Posted 2 years ago by meeshka

I have a PaymentTransaction and PaymentError models as part of my simple ecommerce project. Each PaymentTransaction may have one or more PaymentErrors. So, I need to fetch the last error along with PaymentTransactions and here is what I am trying. It doesn't seem to provide any results for payment errors. Is there anything wrong here? Don't see any errors though.

PaymentTransaction::recentPayments($userId)
        ->with(['paymentErrors' => function($query) {
            $query->orderBy('created_at', 'desc')->take(1);
        }])
        ->get();

Here is how recentPayments looks.

    public static function recentPayments($userId, $limit=12)
    {
        $payments = static::whereUserId($userId)
                        ->orderBy('created_at', 'desc')
                        ->take($limit);

        return $payments;
    }

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

Reply to

Use Markdown with GitHub-flavored code blocks.