@foreach ($orders as $order)
@php
// Find if this order has payment method id = 4 (from eager loaded collection)
$ordersWithPaymentMethod = $order->order_payments->firstWhere('payment_method_id', 4);
// Get receipt token if exists
$token = null;
$firstPayment = $order->order_payments->first();
if ($firstPayment && $firstPayment->receipt_id) {
$token = $firstPayment->receipt->token ?? null;
}
// If no token, generate one
if (empty($token)) {
do {
$token = $order->id . date('dmYHis') . $order->account_id . Str::random(25);
} while (\App\Models\CustomerFile::where('token', $token)->exists());
}
@endphp