为什么表单元素中的 "required" 属性没有显示为“1”?

Why instead of appearing the "required" attribute in the form element is appearing "1"?

我有一个表单供用户创建自定义问题。用户需要介绍问题以及字段类型(文本、长文本、复选框、select 菜单、单选按钮)以创建自定义问题。

然后我有一个问题模型,它有 getHtmlInput() 函数,可以根据自定义问题类型在视图中输出不同的类型。

它工作正常,问题是 "required" 属性在数据透视表 table "registration_type_questions" 中有列 "required" 为“1”。

你知道为什么吗?

问题模型:

class Question extends Model
{
    protected $fillable = [
        'question', 'type', 'conference_id',
    ];

    public static $typeHasOptions = [
        'radio_btn',
        'select_menu',
        'checkbox'
    ];

    public function registration_type(){
        return $this->belongsToMany('App\RegistrationType', 'registration_type_questions')
            ->withPivot('required');
    }

    public function options() {
        return $this->hasMany('App\QuestionOption');
    }

    public function hasOptions() {
        return in_array($this->type, self::$typeHasOptions);
    }


    public function getHtmlInput($name = "", $options = "", $required = false, $class = "", $customtype = false) {

        $html = '';
        $html .= $customtype == 'select_menu' ? "<select name='participant_question' class='form-control' " . ($required ?: " required") . ">" : '';

        if (empty($options)) {
            switch ($customtype) {


                case "text":

                $html .= " 


                    <input type='text' name='participant_question' class='form-control'" . ($required ?: " required") . ">";


                break;

                case "file":

                    $html .= " 


                    <input type='file' name='participant_question' class='form-control'" . ($required ?: " required") . ">";


                    break;

                case "long_text":
                    $html .= "

                <textarea name='participant_question' class='form-control' rows='3'" . ($required ?: " required") . ">"
                        . $name .
                        "</textarea>";

                    break;
            }
        } else {
            foreach ($options as $option) {
                switch ($customtype) {
                    case "checkbox":
                        $html .= " 
                <div class='form-check'>
                    <input type='checkbox' name='participant_question[]' value='" . $option->value . "' class='form-check-input'" . ($required ?: " required") . ">" .
                            '    <label class="form-check-label" for="exampleCheck1">' . $option->value . '</label>' .
                            "</div>";
                        break;
                    case "radio_btn":
                        $html .= " 
                <div class='form-check'>
                    <input type='radio' name='participant_question[]' value='" . $option->value . "' class='form-check-input'" . ($required ?: " required") . ">" .
                            '    <label class="form-check-label" for="exampleCheck1">' . $option->value . '</label>' .
                            "</div>";
                        break;
                    case "select_menu":
                        $html .= "<option value='" . $option->value . "'>" . $option->value . "</option>";
                        break;
                }
            }
        }
        $html .= $customtype == 'select_menu' ? "</select>" : '';

        return $html;
    }
}

在视图中使用 getHtmlInput():

@if ($allParticipants == 0)
    @foreach($selectedRtype['questions'] as $customQuestion)
        <div class="form-group">
            <label for="participant_question">{{$customQuestion->question}}</label>
            @if($customQuestion->hasOptions() && in_array($customQuestion->type, ['checkbox', 'radio_btn', 'select_menu']))
                {!! $customQuestion->getHtmlInput(
                    $customQuestion->name,
                    $customQuestion->options,
                    ($customQuestion->pivot->required == '1'),
                    'form-control',
                    $customQuestion->type)
                !!}

            @else
                {!! $customQuestion->getHtmlInput(
                    $customQuestion->name,
                    [],
                    ($customQuestion->pivot->required == '1'),
                    'form-control',
                    $customQuestion->type)
                !!}
            @endif
            <input type="hidden"
                   name="participant_question_required[]"
                   value="{{ $customQuestion->pivot->required }}">
            <input type="hidden"
                   value="{{ $customQuestion->id }}"
                   name="participant_question_id[]"/>
        </div>
    @endforeach
@endif

已生成 HTML:(而不是要求出现 1)

<form method="post" action="">
  <div class="form-group">
    <label for="participant_question">Text</label>
    <input type="text" name="participant_question" class="form-control" 1="">
  </div>
  <div class="form-group">
    <label for="participant_question">Checkbox</label>
    <div class="form-check">
      <input type="checkbox" name="participant_question[]" value="check1" class="form-check-input" 1="">    <label class="form-check-label" for="exampleCheck1">check1</label></div> 
    <div class="form-check">
      <input type="checkbox" name="participant_question[]" value="check2" class="form-check-input" 1="">    <label class="form-check-label" for="exampleCheck1">check2</label></div>
  </div>
  <div class="form-group">
    <label for="participant_question">Radio</label>
    <div class="form-check">
      <input type="radio" name="participant_question[]" value="rad1" class="form-check-input" 1="">    <label class="form-check-label" for="exampleCheck1">rad1</label></div> 
    <div class="form-check">
      <input type="radio" name="participant_question[]" value="rad2" class="form-check-input" 1="">    <label class="form-check-label" for="exampleCheck1">rad2</label></div>
  </div>
  <div class="form-group">
    <label for="participant_question">select</label>
    <select name="participant_question" class="form-control" required=""><option value="select1">select1</option><option value="select2">select2</option></select>
  </div>
  <div class="form-group">
    <label for="participant_question">textarea</label>
    <textarea name="participant_question" class="form-control" rows="3" 1=""></textarea>
  </div>
  <div class="form-group">
    <label for="participant_question">file</label>
    <input type="file" name="participant_question" class="form-control" 1="">
  </div>
  <input type="submit" href="#step2" class="btn btn-primary mb-4 float-right next-step" value="Confirm Registration">
</form>

在这段代码中

($required ?: " required")

你没有设置任何东西,因为语句是真的(是必需的)所以它设置了默认值,你告诉它是必需的,而不是,正确的语法是

($required ? " required" : "")