定制 matt-daneshvar/laravel-survey 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

matt-daneshvar/laravel-survey

Composer 安装命令:

composer require matt-daneshvar/laravel-survey

包简介

Create surveys inside your Laravel app

README 文档

README

Tests GitHub

Create and manage surveys within your Laravel app.

Demo

This video walks through installing this package and creating a basic survey.

Installation

Require the package using composer.

composer require matt-daneshvar/laravel-survey

Publish the package migrations.

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

Run the migrations to create all the required tables.

php artisan migrate 

Usage

Creating a Survey

Creating a new Survey is easy! You can build your survey fluently just like how you create all your Eloquent models in your app.

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

$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']
]);

See the list of available question types.

Creating Multiple Sections

You may also park your questions under multiple sections.

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

$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']
]);

Creating an Entry

From an Array

The Entry model comes with a fromArray function.
This is especially useful when you're creating an entry from a form submission.

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

The answer array should be in the format of q + question_id => answer, thus becoming 'q1' => 'my answer'.

By a Specific User

You may fluently specify the participant using the by() function.

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

Setting Constraints

When creating your survey, you may set some constraints to be enforced every time a new Entry is being created.

Allowing Guest Entries

By default, Entry models require a participant_id when being created. If you wish to change this behaviour and accept guest entries, set the accept-guest-entries option on your Survey model.

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

Adjusting Entries Per Participant Limit

All Survey models default to accept only 1 entry per unique participant. You may adjust the limit-per-participant option on your Survey model or set it to -1 to remove this limit altogether.

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

Note that this setting will be ignored if the accept-guest-entries option is activated.

Validation

Defining Validation Rules

Add in a rules attribute when you're creating your Question to specify the validation logic for the answers being received.

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

Note that as opposed to the survey constraints, the question validators are not automatically triggered during the entry creation process. To validate the answers, you should manually run the validation in your controller (see below)

Validating Submissions

Validate user's input against the entire rule set of your Survey using Laravel's built in validator.

class SurveyEntriesController extends Controller
{
    public function store(Request $request, Survey $survey)
    {
        $answers = $request->validate($survey->rules);
        
        (new Entry())->for($survey)->fromArray($answers)->push();
    }
}

Views

This package comes with boilerplate Bootstrap 4.0 views to display the surveys and some basic question types. These views are meant to serve as examples, and may not be sufficient for your final use case. To display a survey in a card, include the survey partial in your views:

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

Question Types

These are the question types included out of the box:

  • text - Accepting text answers
  • number - Accepting numeric answers
  • radio - Options presented as radio buttons, accepting 1 option for the answer
  • multiselect - Options presented as checkboxes, accepting multiple options for the answer

Customizing the Views

To customize the boilerplate views shipped with this package run package:publish with the views tag.

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

This will create a new vendor/survey directory where you can fully customize the survey views to your liking.

Creating New Question Types

Once you publish the views that come with this package, you can add your own custom question types by implementing new templates for them.

To implement a new custom-select type, for example, you should implement a new template under:

<your-views-director>/vendor/survey/questions/types/custom-select.blade.php

License

The MIT License (MIT). Please see License File for more information.

matt-daneshvar/laravel-survey 适用场景与选型建议

matt-daneshvar/laravel-survey 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 79.51k 次下载、GitHub Stars 达 287, 最近一次更新时间为 2019 年 08 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「survey」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 matt-daneshvar/laravel-survey 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 matt-daneshvar/laravel-survey 我们能提供哪些服务?
定制开发 / 二次开发

基于 matt-daneshvar/laravel-survey 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 79.51k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 287
  • 点击次数: 23
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 287
  • Watchers: 7
  • Forks: 66
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-08-22