wimando/laravel-survey

在您的 Laravel 应用中创建调查

v0.0.7 2021-11-28 15:10 UTC

This package is auto-updated.

Last update: 2024-09-28 21:36:20 UTC


README

创建和管理 Laravel 应用中的调查。我决定从这个 https://github.com/matt-daneshvar/laravel-survey 分支出来,并修改以满足项目需求。

安装

使用 composer 安装此包。

composer require wimando/laravel-survey

发布包迁移。

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

运行迁移以创建所有调查表。

php artisan migrate 

使用

创建调查

创建新的 Survey 很简单!您可以使用流畅的方式构建调查,就像在您的应用中创建所有 Eloquent 模型一样。

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

$survey->save();

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

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

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

创建多个部分

您也可以将问题放在多个部分下。

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

$survey->save();

$one = $survey->sections()->create(['name' => 'Part One']);

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

$two = $survey->sections()->create(['name' => 'Part Two']);

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

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

创建条目

从数组中

Entry 模型包含一个 fromArray 函数。
当您从表单提交中创建条目时,这特别有用。

$entry = EntryFactory::create();

$entry->for($survey)->fromArray([
    'q1' => 'Yes',
    'q2' => 5
])->push();

指定特定用户

您可以使用 by() 函数流畅地指定参与者。

$entry = EntryFactory::create();

$entry->for($survey)->by($user)->fromArray($answers)->push();

设置约束

当创建调查时,您可以设置一些约束,以便在创建每个新的 Entry 时强制执行。

允许访客条目

默认情况下,创建 Entry 模型时需要 participant_id。如果您希望更改此行为并接受访客条目,请将 Survey 模型上的 accept-guest-entries 选项设置为。

SurveyFactory::create(['settings' => ['accept-guest-entries' => true]]);

调整每个参与者的条目限制

所有 Survey 模型默认只接受每个唯一参与者 1 个条目。您可以在 Survey 模型上调整 limit-per-participant 选项,或将它设置为 -1 以完全删除此限制。

SurveyFactory::create(['settings' => ['limit-per-participant' => 1]]);

请注意,如果启用了 accept-guest-entries 选项,此设置将被忽略。

验证

定义验证规则

在创建 Question 时添加 rules 属性以指定接收到的答案的验证逻辑。

QuestionFactory::create([
    'content' => 'How many cats do you have?', 
    'rules' => ['numeric', 'min:0']
]);

请注意,与调查约束不同,问题验证器在条目创建过程中不会自动触发。为了验证答案,您应在控制器中手动运行验证(见下文)

验证提交

使用 Laravel 内置验证器验证用户输入与 Survey 的整个规则集。

class SurveyEntriesController extends Controller
{
    public function store(Survey $survey, Request $request)
    {
        $answers = $this->validate($request, $survey->rules);
        
        $entry = EntryFactory::create();
        
        $entry->for($survey)->fromArray($answers)->push();
    }
}

视图

默认情况下,此包附带用于显示调查和一些基本问题类型的 Bootstrap 4.0 视图模板。这些视图仅用作示例,不应在生产中替换您的视图。要在卡片中显示调查,请在您的视图中包含 card 部分视图。

@include('survey::standard', ['survey' => $survey])

自定义视图

要自定义此包附带的自定义视图,请使用带有 views 标签的 package:publish

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

这将创建一个新的 vendor/wimando/survey 目录,您可以在其中完全自定义调查视图。

路线图

  • 允许配置。
  • Entry 中泛化参与者关系。
  • 为选项添加权重。
  • 实现向导+部分分页!
  • 支持匿名条目。
  • 添加管理仪表板。

许可

MIT 许可证 (MIT)。有关更多信息,请参阅 许可文件