Installation:
composer require baks-dev/support-answer
Publish the package config and migrations:
php artisan vendor:publish --provider="BaksDev\SupportAnswer\SupportAnswerServiceProvider" --tag="config"
php artisan vendor:publish --provider="BaksDev\SupportAnswer\SupportAnswerServiceProvider" --tag="migrations"
php artisan migrate
First Use Case:
config/support-answer.php under the templates key:
'templates' => [
'password_reset' => [
'subject' => 'Password Reset Instructions',
'body' => 'Hello {name}, here is your reset link: {link}',
],
],
use BaksDev\SupportAnswer\Facades\SupportAnswer;
$response = SupportAnswer::send('password_reset', [
'name' => 'John Doe',
'link' => url('reset-password?token=abc123'),
]);
Where to Look First:
config/support-answer.php (templates, default settings).SupportAnswer (main entry point for sending answers).database/migrations/*support_answer* (check table structure for custom fields).tests/Feature/SupportAnswerTest.php (real-world usage examples).Template-Based Responses:
SupportAnswer::send('invoice_confirmation', [
'order_id' => $order->id,
'amount' => $order->total,
]);
default template in config for uncaught cases.Integration with Laravel Mail:
Mailable system. Override the default mail driver in config:
'mail_driver' => 'smtp', // or 'log', 'array', etc.
SupportAnswerMailable class for complex logic:
namespace App\Mail;
use BaksDev\SupportAnswer\Mail\SupportAnswerMailable;
class CustomSupportAnswer extends SupportAnswerMailable {
public function build() {
return $this->subject('Custom Subject');
}
}
Register in config/support-answer.php:
'custom_mailables' => [
'custom_template' => \App\Mail\CustomSupportAnswer::class,
],
Queueing and Events:
SupportAnswer::sendQueued('password_reset', $data);
SupportAnswerSent) to log or notify:
use BaksDev\SupportAnswer\Events\SupportAnswerSent;
event(new SupportAnswerSent($template, $data));
API Responses:
$response = SupportAnswer::sendJson('api_error', [
'error' => 'Invalid request',
]);
// Returns: { "status": "sent", "template": "api_error", "data": {...} }
Dynamic Template Loading:
Load templates from a database or external API by implementing BaksDev\SupportAnswer\Contracts\TemplateLoader:
class DatabaseTemplateLoader implements TemplateLoader {
public function get(string $name): array {
return DB::table('support_templates')->where('name', $name)->first();
}
}
Bind in AppServiceProvider:
$this->app->bind(
TemplateLoader::class,
DatabaseTemplateLoader::class
);
Localization:
Support multiple languages by extending the SupportAnswer facade:
SupportAnswer::setLocale('ru');
SupportAnswer::send('welcome', $data);
Template Syntax Conflicts:
{ and } in template bodies unless escaped (e.g., {{ and }} for literal braces).{{variable}} for literal output or escape with \{/\}.Queue Stuck Jobs:
support_answer:work queue worker is running:
php artisan queue:work --queue=support_answer
failed_jobs table for errors; log SupportAnswerFailed events.Missing Config:
php artisan vendor:publish again and php artisan migrate.Caching Issues:
php artisan cache:clear
php artisan config:clear
Character Encoding:
smtp with charset=utf-8).Log Templates: Enable debug mode in config:
'debug' => true,
Logs will appear in storage/logs/laravel.log.
Test with log Driver:
Temporarily set mail_driver to log to verify templates:
'mail_driver' => 'log',
Check storage/logs/laravel.log for rendered emails.
Validate Data:
Use the validate method to ensure data matches template placeholders:
SupportAnswer::send('template', $data)->validate();
// Throws \InvalidArgumentException if placeholders are missing.
Custom Storage:
Override template storage by binding BaksDev\SupportAnswer\Contracts\TemplateRepository:
$this->app->bind(TemplateRepository::class, CustomTemplateRepository::class);
Pre/Post-Send Hooks: Use events to modify behavior:
// Before sending
SupportAnswer::beforeSend(function ($template, $data) {
$data['timestamp'] = now()->toDateTimeString();
});
// After sending
SupportAnswer::afterSend(function ($response) {
Analytics::log('support_answer_sent', $response->template);
});
API Rate Limiting:
Protect the sendJson endpoint with Laravel’s throttle middleware:
Route::middleware(['throttle:10,1'])->post('/api/support', [SupportAnswerController::class, 'send']);
Multi-Channel Support:
Extend to SMS or push notifications by implementing BaksDev\SupportAnswer\Contracts\Channel:
class SmsChannel implements Channel {
public function send(string $template, array $data): bool {
// Logic to send SMS via Twilio/other
}
}
Register in config:
'channels' => [
'sms' => \App\Channels\SmsChannel::class,
],
Use:
SupportAnswer::send('sms_template', $data, 'sms');
Template Versioning:
Add a version field to templates in the database to handle updates without breaking existing data:
'templates' => [
'password_reset_v2' => [
'version' => 2,
'subject' => 'Updated Reset Instructions',
// ...
],
],
Implement logic in TemplateLoader to fetch the latest version.
How can I help you explore Laravel packages today?