In this episode, we tackle how to generate a CSV file and attach it to an outgoing email using Laravel Excel. The goal is to send a monthly payout report via email with all the relevant referral payout data neatly formatted in a CSV for easy processing (e.g., in Excel or for direct import into PayPal).
We start by installing the Laravel Excel package and walking through the basics of how it works. You'll see how we can pass arrays, collections, or even Eloquent queries to build up the raw data for our CSV. We set up the necessary export class to control exactly what data ends up in the CSV, including formatting the amount field to properly display dollar values instead of raw cents.
Once the export is in place and mapped to output the right columns (like PayPal email, amount, and currency), we show how to wire this export up so it's generated on the fly and attached to the outgoing email—no file saved on disk required. We do a few rounds of testing, first with dummy data and then pulling in actual values from the database, making sure the fields are correct and appropriately formatted.
To wrap up, we talk through marking payouts as "paid" after the email sends, ensuring that users don't get duplicate payout notifications. In the end, you'll have a working system that emails out a CSV each month, formatted just how you need it, and smart enough not to double-send.
It's a really practical episode packed with steps you can follow directly. By the end, you'll have a re-usable pattern for generating and emailing CSV reports straight from your Laravel app.