Send::create() and integrates with Laravel’s events (MailSent), enabling hooks for analytics, retries, or third-party integrations (e.g., Mailgun/SendGrid webhooks).spatie/laravel-activitylog) for full lifecycle management.Mail facade and Eloquent relationships, requiring minimal configuration (publish migrations, add traits to models).sends table with foreign keys to mailables and models, which must be accounted for in schema migrations. Indexing on mailable_type/mailable_id and model_type/model_id is critical for performance.Mail facade and Send models in unit tests. Integration tests should verify relationships and query performance.sends table require partitioning or archiving?Send::create().log (which bypasses tracking).Mail facade to use queues; direct SwiftMailer calls won’t trigger tracking.MailFake for unit tests.composer require wnx/laravel-sends
php artisan vendor:publish --provider="Wnx\LaravelSends\LaravelSendsServiceProvider"
php artisan migrate
HasSends trait to Eloquent models:
use Wnx\LaravelSends\HasSends;
class User extends Model { use HasSends; }
Send::create([
'mailable_type' => ProductReviewMail::class,
'model_type' => User::class,
'model_id' => $user->id,
'metadata' => ['review_id' => $review->id],
]);
sends() relationship:
$user->sends()->with('mailable')->get();
Send::forMailClass(ProductReviewMail::class)->where('metadata->review_id', 1)->get();
mailable_type column naming).mailables table.Send model observer for soft-deletes).MailSent events for troubleshooting. Common issues:
HasSends.sends() queries (add indexes).sends()).MailSent event listener).mailable_type, mailable_id, model_type, model_id, and created_at for common queries.Send::create() calls if sending emails in loops (e.g., bulk notifications).user->sends()->count()) with remember() or Redis.sends table size. Implement:
sends_archive table via queued job.SendObserver or Laravel’s SoftDeletes.| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Database connection drops | Lost email records | Use transactions for Mail::send() + Send::create(). |
| Queue worker crashes | Delayed/failed email tracking | Retry logic in MailSent event listener. |
| Model association errors | Orphaned Send records |
Validate models exist before associating. |
| High query load | Slow sends() relationships |
Add indexes, use with(), or denormalize data. |
| Migration conflicts | Broken schema | Test migrations in staging; use --force cautiously. |
Send::create() vs. automatic tracking.sends()).sends table.How can I help you explore Laravel packages today?