Level 51
@meeshka see https://softonsofa.com/tweaking-eloquent-relations-how-to-get-latest-related-model/ for a great example
1 like
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;
}
@meeshka see https://softonsofa.com/tweaking-eloquent-relations-how-to-get-latest-related-model/ for a great example
Please or to participate in this conversation.