Search by

nikoleesg / survey

nikoleesg

Store and query survey variables, answers and paradata for Laravel

Package info

github.com/nikoleesg/survey

pkg:composer/nikoleesg/survey

Statistics

Installs: 61

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 6

2.0.0 2025-12-04 05:08 UTC

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

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 (Data facade): 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 (Answer facade): answers grouped by interview, keyed by variable slug, with DATETIME values hydrated to Carbon in a configurable timezone.
  • Extensible: every model is bound in config/survey.php, so an application can subclass Sample and 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.