如何接收多个复选框值并在数据库中正确存储?

时间:2018-03-16 15:36:28

标签: php laravel

我的表单中有一些字段/问题(您的电子邮件是什么?,您的电话号码是什么?)然后用户可以选择是否应该为会议的注册类型包含该字段,以及对于每种注册类型,通过复选框,字段应该是强制的。表单布局示例:

enter image description here

例如,对于会议"测试会议"有两种注册类型(RT01和RT02)和两个问题"你的电子邮件是什么?"和#34;你的手机是什么?"。

在用户可以为每种注册类型选择的形式中,如果他想要包含两个问题,只需要一个,没有,并且对于每个包含的问题,用户可以选择是否对每个注册类型强制该问题。

所以,例如,如果对于问题"你的电子邮件是什么?"所有复选框("包含注册类型RT01","包括注册类型RT02和#34;,"注册类型RT01和#34必须;"强制性注册类型RT02")被检查,并且问题"您的手机是什么?"选中所有复选框后,应将其插入数据透视表" registration_type_questions"像这样:

registration_type_id question_id required
 1                       1        1      (Include the question with id 1 in the registrationtype with id 1 and the question is mandatory for the registrationtype with id 1)
 2                       1        1    
  1                       2        1       
  2                       2        1 

但插入如下:

registration_type_id question_id required
  1                       2        0       
  2                       2        0     

你知道问题出在哪里吗?我现在的代码是:

用于编辑会议注册表单的表单:

  <form method="post" class="clearfix" action="{{route('questions.update', ['conf_id' => $conf->id])}}">

      {{csrf_field()}}

      <table class="table table-striped table-responsive-sm">
    <thead>
    <tr>
        <th scope="col">Info</th>
        <th scope="col">Include for registration type</th>
        <th scope="col">Mandatory field</th>
    </tr>
    </thead>
    <tbody>

    @foreach($question as $q)
        <tr>
            <td>{{$q->question}}</td>
            <td>
                @foreach($registration_types as $rtype)
                    <div class="form-check">
                        <input autocomplete="off" name="question[{{$q->id}}][rtypes][]" class="form-check-input {{$rtype->name}}" type="checkbox" value="{{ $rtype->id }}" id="{{$rtype->id}}">
                        <label class="form-check-label" for="exampleRadios1">
                            {{$rtype->name}}
                        </label>
                    </div>
                @endforeach
            </td>
            <td>
                @foreach($registration_types as $rtype)
                    <div class="form-check">
                        <input autocomplete="off"  name="question[{{$q->id}}][mandatories][]"
                               class="form-check-input mandatorycheckbox" type="checkbox" value="{{ $rtype->id }}" id="{{$rtype->id}}">
                        <label class="form-check-label" for="exampleRadios1">
                            for the registration type "{{$rtype->name}}"
                        </label>
                    </div>
                @endforeach
            </td>
        </tr>
    @endforeach

    </tbody>
</table>      

      <input type="submit" class="btn btn-primary float-right mt-3" value="Update"/>
    </form>   

问题模型

class Question extends Model
{

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

注册类型模型

class RegistrationType extends Model
{
    public function conference(){
        return $this->belongsTo('App\Conference');
    }

    public function questions(){
        return $this->belongsToMany('App\Question', 'registration_type_questions');
    }
}

会议模式

class Conference extends Model
{
   // A conference has many registration types
    public function registrationTypes(){
        return $this->hasMany('App\RegistrationType', 'conference_id');
    }
}

QuestionController更新方法:

 public function update(Request $request, $question_id){
        $question = $request->get('question');

        foreach($question as $key => $q) {
                //with "$outputArray = array_fill_keys($q["rtypes"], $q["mandatories"]);" it appears Undefined index: mandatories
            $outputArray = array_fill_keys($q["rtypes"], ["required" => false]);
            foreach ($outputArray as $lineKey => $line) {
                $line[$lineKey]["required"] = in_array($lineKey, $q["mandatories"]);
            }
        }

        $getQuestion = Question::find($key);
        if($getQuestion);
        $getQuestion->registration_type()->sync($outputArray);

        $this->validate($request, [
            ]);
    }

QuestionController中的$questions&#34; foreach($question as $key => $q) {&#34;有这个内容:

array:2 [▼
  1 => array:2 [▼
    "rtypes" => array:2 [▼
      0 => "1"
      1 => "2"
    ]
    "mandatories" => array:2 [▼
      0 => "1"
      1 => "2"
    ]
  ]
  2 => array:2 [▼
    "rtypes" => array:2 [▼
      0 => "1"
      1 => "2"
    ]
    "mandatories" => array:2 [▼
      0 => "1"
      1 => "2"
    ]
  ]
]

2 个答案:

答案 0 :(得分:0)

您需要通过public List<Foods> food = new ArrayList<>();在模型中声明您的数据透视。

->withPivot

然后你可以像这样更新它:

class Question extends Model
{

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

$getQuestion->registration_type()->updateExistingPivot($id, ['required' => 1]); 变量应包含registration_type id。

答案 1 :(得分:0)

试试这个...... [表单和控制器都已更新]

更新了注册表单

@foreach($question as $q)
    <tr>
        <td>{{$q->question}}</td>
            <td>
                @foreach($registration_types as $rtype)
                    <div class="form-check">
                        <input autocomplete="off" name="include[{{ $q->id }}][{{ $rtype->id }}]" class="form-check-input {{$rtype->name}}" type="checkbox" value="1" id="{{$rtype->id}}">
                            <label class="form-check-label" for="exampleRadios1">
                                {{$rtype->name}}
                            </label>
                    </div>
                @endforeach
            </td>
            <td>
                @foreach($registration_types as $rtype)
                    <div class="form-check">
                        <input autocomplete="off"  name="mandatory[{{ $q->id }}][{{ $rtype->id }}]"
                                   class="form-check-input mandatorycheckbox" type="checkbox" value="1" id="{{$rtype->id}}">
                            <label class="form-check-label" for="exampleRadios1">
                                for the registration type "{{$rtype->name}}"
                            </label>
                        </div>
                    @endforeach
                </td>
            </tr>
        @endforeach

更新了控制器

public function update(Request $request, $question_id)
{
    $includeArray = [];
    $includes     = $request->get('include');
    $mandatory    = $request->get('mandatory');

    //fetch all questions using required where conditions
    $questions = Question::get();    

    foreach($questions as $key => $question) { 
        if(!empty($includes[$question->id])) { 
            foreach($includes[$question->id] as $include => $value) { 
                if (!empty($mandatory[$question->id][$include]) && $mandatory[$question->id][$include] == 1) { 
                    $includeArray[$include] = ['required' => 1]; 
                } else { 
                    $includeArray[$include] = ['required' => 0]; 
                } 
            } 
        } else if(!empty($mandatory[$question->id])) { 
            foreach($mandatory[$question->id] as $mandatoryKey => $value) { 
                if (!empty($mandatory[$question->id][$mandatoryKey]) && $mandatory[$question->id][$mandatoryKey] == 1) { 
                    $includeArray[$mandatoryKey] = ['required' => 1]; 
                } else { 
                    $includeArray[$mandatoryKey] = ['required' => 0]; 
                } 
            } 
        } 
        $question->registration_type()->sync($includeArray); 
        $includeArray = []; 
    } 
}