nikoleesg / survey
Store and query survey variables, answers and paradata for Laravel
Requires
- php: ^8.1
- illuminate/contracts: ^10.0
- larautil/foundation: ^1.0
- spatie/eloquent-sortable: ^4.0
- spatie/laravel-data: ^4.0
- spatie/laravel-enum: ^3.0
- spatie/laravel-package-tools: ^1.14.0
- spatie/laravel-sluggable: ^3.5
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^7.8
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^8.8
- pestphp/pest: ^2.20
- pestphp/pest-plugin-arch: ^2.0
- pestphp/pest-plugin-laravel: ^2.0
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
A Laravel package that loads fixed-width survey data exports (closed answers, open/verbatim answers and paradata) into four relational tables, then reads them back as typed, slug-keyed answers.
- Write path (
Datafacade): parse the closed-answer file by each variable's position, length and type; match verbatim rows to open variables; read UTF-16 paradata. Then upsert everything in chunks, creating stub samples when open answers or paradata arrive before the closed file. - Read path (
Answerfacade): answers grouped by interview, keyed by variable slug, withDATETIMEvalues hydrated to Carbon in a configurable timezone. - Extensible: every model is bound in
config/survey.php, so an application can subclassSampleand add its own columns.
PHP ^8.3 · Laravel 12/13 · Pest + Larastan (level 4) + Pint.
Installation
You can install the package via composer:
composer require nikoleesg/survey
You can publish and run the migrations with:
php artisan vendor:publish --tag="survey-migrations"
php artisan migrate
You can publish the config file with:
php artisan vendor:publish --tag="survey-config"
Usage
use Nikoleesg\Survey\Facades\Answer; use Nikoleesg\Survey\Facades\Data; // Load one wave's exports and upsert them (variables for the survey must already exist). Data::setSurvey('wave-2026-01') ->getClosedAnswersFromFile(storage_path('exports/closed.dat')) ->persist(); Data::getOpenAnswersFromFile(storage_path('exports/open.dat'), 'wave-2026-01')->persist(); Data::getParadatafromFile(storage_path('exports/paradata.txt'), 'wave-2026-01')->persist(); // Read answers back, grouped by interview number and keyed by variable slug. $answers = Answer::setSurvey('wave-2026-01')->getAnswers([1001, 1002]);
Exceptions
Every exception the package throws extends Nikoleesg\Survey\Exceptions\SurveyException (a RuntimeException), so a single catch (SurveyException $e) covers them all.
| Exception | Thrown when |
|---|---|
MissingSurveyIdException |
No survey id could be resolved: none passed, none set via setSurvey(), and survey.survey_id is null or empty. |
UnreadableFileException |
A get*FromFile() loader is given a path that does not exist or cannot be read. The message names the loader and the path. |
NoDataLoadedException |
persist() or getData() is called before any get*FromFile() loader. |
UnparseableColumnException |
A non-blank DATETIME/DATE/TIME column in the closed-answer file does not match its survey.formats.* pattern. The message names the interview, variable, raw value and format. Blank columns are stored as null, not thrown. |
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.