Installation
composer require xlabs/surveybundle
Add to config/app.php under providers:
Xlabs\SurveyBundle\SurveyBundle::class,
Publish the bundle’s assets and config:
php artisan vendor:publish --provider="Xlabs\SurveyBundle\SurveyBundle" --tag=config
php artisan vendor:publish --provider="Xlabs\SurveyBundle\SurveyBundle" --tag=assets
First Use Case: Creating a Survey
config/survey.php or via migrations:
// Example: config/survey.php
'surveys' => [
'customer_satisfaction' => [
'title' => 'Customer Satisfaction',
'questions' => [
['type' => 'text', 'question' => 'How was your experience?'],
['type' => 'rating', 'question' => 'Rate our service (1-5)', 'min' => 1, 'max' => 5],
],
],
],
use Xlabs\SurveyBundle\SurveyManager;
public function showSurvey(SurveyManager $manager)
{
$survey = $manager->getSurvey('customer_satisfaction');
return view('surveys.show', compact('survey'));
}
Basic Routing
Configure routes in routes/web.php:
use Xlabs\SurveyBundle\Http\Controllers\SurveyController;
Route::prefix('surveys')->group(function () {
Route::get('/{slug}', [SurveyController::class, 'show']);
Route::post('/{slug}/submit', [SurveyController::class, 'submit']);
});
// In a controller or service
$surveyManager = app(SurveyManager::class);
$survey = $surveyManager->getSurvey('dynamic_survey', ['user_id' => auth()->id()]);
getSurvey() in a custom service to fetch from a database or API.text, rating, multiple_choice, checkbox, date.SurveyBundle's Question class:
// Example: Add validation to a question
$question = new Question([
'type' => 'text',
'question' => 'What did you like?',
'rules' => 'required|min:10',
]);
Xlabs\SurveyBundle\Question\Question and register in config/survey.php:
'custom_question_types' => [
'custom_type' => \App\CustomQuestion::class,
],
SurveyResponse model (auto-generated by the bundle) or extend it:
// Example: Submit a response
$response = $surveyManager->submitResponse('customer_satisfaction', [
'answers' => [
'q1' => 'Great experience!',
'q2' => 4,
],
'metadata' => ['user_id' => auth()->id()],
]);
$responses = SurveyResponse::where('survey_slug', 'customer_satisfaction')->get();
resources/views/vendor/survey/) or override them:
@extends('surveys.layout')
@section('content')
@include('survey::form', ['survey' => $survey])
@endsection
// In a controller
return view('surveys.custom_form', [
'questions' => $survey->getQuestions()->map(fn($q) => $q->toArray()),
]);
$avgRating = SurveyResponse::where('survey_slug', 'customer_satisfaction')
->avg('answers->q2'); // Assuming 'q2' is the rating question
SurveyResponse model or create a service to process raw data.// resources/lang/en/survey.php
return [
'customer_satisfaction' => [
'title' => 'Customer Satisfaction (EN)',
'questions' => [
'q1' => 'How was your experience? (EN)',
],
],
];
config/survey.php if translations are missing.config/survey.php for typos in the surveys array.php artisan config:clear if changes aren’t reflected.InvalidQuestionTypeException. Ensure all question types are registered in config/survey.php under custom_question_types.// In a controller
$validator = Validator::make($request->all(), $survey->getValidationRules());
if ($validator->fails()) {
\Log::error('Survey validation failed:', $validator->errors());
}
survey_responses table exists (run migrations if needed).// In a service class
private $surveys = [];
public function getSurvey(string $slug): Survey
{
if (!isset($this->surveys[$slug])) {
$this->surveys[$slug] = $surveyManager->getSurvey($slug);
}
return $this->surveys[$slug];
}
SurveyResponse::where('survey_slug', 'customer_satisfaction')->chunk(100, function ($responses) {
// Process chunk
});
SurveyManager to fetch surveys from an API or NoSQL:
// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->bind(SurveyManager::class, function ($app) {
return new \App\Services\CustomSurveyManager();
});
}
SurveySubmitted):
// In EventServiceProvider
protected $listen = [
\Xlabs\SurveyBundle\Events\SurveySubmitted::class => [
\App\Listeners\LogSurveyResponse::class,
],
];
@csrf in Blade templates or middleware.config/survey.php.file question types, configure storage in config/filesystems.php and validate uploads manually.SurveyManager to test controllers:
$this->mock(SurveyManager::class)->shouldReceive('getSurvey')
->once()->andReturn($mockSurvey);
actingAs() for authenticated survey submissions:
$response = $this->actingAs($user)->post('/surveys/customer_satisfaction/submit', [
'answers' => ['q1' => 'Test'],
]);
$response->assertRedirect('/thank-you');
How can I help you explore Laravel packages today?