Files
cheerstothe_season_2.0/app/Exports/customer_export.php
sayliraut 0351840e0c changes
2024-08-12 16:12:44 +05:30

68 lines
2.2 KiB
PHP

<?php
namespace App\Exports;
use Maatwebsite\Excel\Concerns\FromCollection;
use App\Models\IamPrincipal;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Illuminate\Support\Collection;
class customer_export implements FromCollection, WithHeadings
{
/**
* @return \Illuminate\Support\Collection
*/
public function collection()
{
$customers = IamPrincipal::where('principal_type_xid', 3)
->with(['isSubscribed', 'state'])
->select(
'id',
'first_name',
'last_name',
'email_address',
'date_of_birth',
'state_xid',
'phone_number'
)
->get();
$serial = 1;
return $customers->map(function ($customer) use (&$serial) {
$subscription = $customer->isSubscribed->first();
$dateTime = now();
$formattedDateTime = $dateTime->format('Y-m-d H:i:s');
$isSubscribed = $subscription && $subscription->next_payment_date >= $formattedDateTime && $subscription->status == 'complete' ? 'Subscribed' : 'Unsubscribed';
return [
'Sr No.' => $serial++, // Increment serial number
'id' => $customer->id,
'first_name' => $customer->first_name,
'last_name' => $customer->last_name,
'email_address' => $customer->email_address,
'date_of_birth' => \Carbon\Carbon::parse($customer->date_of_birth)->format('m/d/Y'),
'state_name' => $customer->state->name ?? '', // Access the state name and handle null
'phone_number' => $customer->phone_number,
'subscription_status' => $isSubscribed, // Add the subscription status
];
});
}
// Function to provide the headings in Excel
public function headings(): array
{
return [
'Sr No.',
'User Id',
'First Name',
'Last Name',
'Email Address',
'Date of Birth',
'State Name',
'Phone Number',
'Subscription Status',
];
}
}