首页 > 解决方案 > 如何使用 Laravel 将带有 yes no 复选框的表设置为 mysql

问题描述

请帮我解决这个问题:使用 Laravel

访客(无注册/登录) - 完成 <有登录/注册 - 工作>

带有基本文本字段的调查表:fname、lname、ID 号、移动电话,然后是 3 个带有复选框的yes问题。no

嗨这是我的迁移 create_survey_posts_table.php


use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateSurveyPostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->string('fname');
            $table->string('lname');
            $table->string('id_number');
            $table->string('mobile');
            $table->string('sq-symptoms');
            $table->string('sq-travel');
            $table->string('sq-contact');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('survey_posts');
    }
}

我应该使用什么,sq-symptoms因为它是一个单选按钮yesno上传(迁移)到 mysql 的选项

标签: phpmysqllaravel

解决方案


带有验证的无线电输入可能吗?

<fieldset>
  <legend>Symptoms</legend>
  <label>Yes<input type="radio" name="sq_symptoms" value="Yes"></label>
  <label>No<input type="radio" name="sq_symptoms" value="No"></label>
</fieldset>

// laravel validation
sq_symptoms: ['required', 'in:Yes,No']
// using the Rule facade
sq_symptoms: ['required', Rule::in(['Yes', 'No'])]

此示例中不需要forid属性。为了解释原因,这里是MDN 关于<label>元素的文档的摘录:

要将<label><input>元素相关联,您需要提供 <input>anid属性。然后<label>需要一个for属性,其值与输入的 相同id

或者,您可以将<input>直接嵌套在 中<label>,在这种情况下,不需要forand属性,因为关联是隐式的:id


推荐阅读