Matt Daneshvar’s Survey module for Laravel – easy web surveys without a SurveyMonkey account

The Laravel-Survey module is an easy way to create a survey with results logged into a Laravel application.

The instructions given on the Github readme are nearly complete yet seem to miss a step or two. To set it up, start with the instructions they provide:

composer require matt-daneshvar/laravel-survey

within the project folder. Then:

php artisan vendor:publish --provider="MattDaneshvar\Survey\SurveyServiceProvider" --tag="migrations" 
php artisan vendor:publish --provider="MattDaneshvar\Survey\SurveyServiceProvider" --tag="views"

after copying their survey.php to config/ directory, run:

php artisan config:clear
php artisan migrate
php artisan make:controller SurveyController
php artisan make:migration mysurveyclassname

For “mysurveyclassname” being your class migration to make a new survey or surveys. If you get an error like,

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'json null, `rules` json null,
...
PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'json null, `rules` json null, `created_at` timestamp null, `updated_at` timestam' at line 1")

try changing “->json” to “->text” for text column type in create_questions_table, create_surveys_table if you are using MariaDB instead of MySQL.

As shown in the documentation, add a Survey::create code in the up() function, adding any number of questions… for example:

$survey = Survey::create(['name' => 'Dog Population Survey']);

$survey->questions()->create([
     'content' => 'How many dogs do you have?',
     'type' => 'number',
     'rules' => ['numeric', 'min:0']
 ]);

$survey->questions()->create([
    'content' => 'What\'s the name of your first dog',
]);

$survey->questions()->create([
    'content' => 'Would you want a new dog?',
    'type' => 'radio',
    'options' => ['Yes', 'Oui']
]);

with the include of the library at the top of the file:

use MattDaneshvar\Survey\SurveyServiceProvider;
use MattDaneshvar\Survey\Models\Survey;

Now add to routes/web.php:

Route::get('/mysurveyurl', 'SurveyController@survey');
Route::post('/mysurveyurl', 'SurveyController@store');

and add the relevant code to SurveyController.php:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use MattDaneshvar\Survey\Models\Survey;
use MattDaneshvar\Survey\Models\Entry;

class SurveyController extends Controller
{
    
	public function survey() {
		$survey = Survey::where(['name' => 'Named survey'])->first();
		return View('mysurveyviewname')->with([
			'survey'=> $survey
		]);
	}
	
	public function store(Request $request)
    {
		$survey = Survey::where(['name' => 'Named survey'])->first();
        $answers = $this->validate($request, $survey->rules );
        
        (new Entry)->for($survey)->fromArray($answers)->push();
		return View('surveythankyou');
    }
}


for the survey mysurveyviewname.blade.php you add in resources/views folder, and the surveythankyou.blade.php in the same folder with a thank you message.

Note that the line in the documentation, “@include(‘survey::standard’, [‘survey’ => $survey])”, should be added where you want the survey to display in the page – after the header, before the footer where applicable. To get it to post to same page, include it in a form with the csrf protection like so:

		<form method="post">
		@csrf
		@include('survey::standard', ['survey' => $survey])
		</form>

Browsing Submitted Results

There isn’t currently a frontend management page for Laravel-survey (I see it is on the roadmap accoring to the readme), so one must get the log of submission entries using an SQL query. For example:

SELECT entry_id,content,value FROM answers, questions
WHERE answers.question_id = questions.id;

Leave a Reply

Your email address will not be published. Required fields are marked *

42 + = fifty one