如何在数据透视表中引入必要的信息?

问题描述 投票:0回答:1

在用户点击“更新”按钮后,需要更新会议注册表单以在数据透视表“registration_type_question”中引入:

  • 所选复选框的“registration_type_id”
  • 每个问题的“question_id”
  • 如果是基于所选复选框(选定的注册类型)的每种注册类型的必需问题

布局示例:https://ibb.co/kftnPc

pivot表“registration_type_questions”列:registration_type_id,question_id,required(boolean)

数据透视表“registration_type_questions”没有模型。我在QuestionController中有更新方法,根据选中的复选框更新注册表单,但我不知道如何在数据透视表中插入必要的信息,你知道怎么做吗?

该问题的相关关系:

 1 to many between RegistrationType and RegistrationTypeQuestions
 1 to may between Question and RegistrationTypeQuestions 
 1 to many between Conference and Question

完整形式

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

  {{csrf_field()}}

  <div class="form-row">
    <div class="form-group col">
      <div class="row">
        <div class="col">
          <table class="table table-striped table-responsive-sm">
            <thead>
              <tr>
                <th scope="col">Info</th>
                <th scope="col">Include in registration type</th>
                <th scope="col">Mandatory Field</th>
              </tr>
            </thead>
            <tbody>
              <tr>
                <td>Name</td>
                <td>
                  <i class='fa fa-check font-size-lg text-primary ml-4'></i>
                </td>
                <td>
                  <i class='fa fa-check font-size-lg text-primary ml-4'></i>
                </td>
              </tr>
              <tr>
                <td>Surname</td>
                <td>
                  <i class='fa fa-check font-size-lg text-primary ml-4'></i>
                </td>
                <td>
                  <i class='fa fa-check font-size-lg text-primary ml-4'></i>
                </td>
              </tr>

              @foreach($question as $q)
              <tr>
                <td>{{$q->question}}</td>
                <td>
                  @foreach($registration_type as $rtype)
                  <div class="form-check">
                    <input name="rypes[]" autocomplete="off" class="form-check-input" type="checkbox" value="{{ $rtype->id }}" id="{{$rtype->id}}">
                    <label class="form-check-label" for="exampleRadios1">
                      {{$rtype->name}}
                    </label>
                  </div>
                  @endforeach
                </td>
                <td>
                  @foreach($registration_type as $rtype)
                  <div class="form-check">
                    <input name="mandatories[]" autocomplete="off" class="form-check-input" 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>
        </div>
      </div>
    </div>
  </div>
  <input type="submit" class="btn btn-primary float-right mt-3" value="Update"/>
</form>

QuestionController更新方法:

public function update(Request $request, $id){

        $values = $request->get('rtypes');
        $mandatories = $request->get('mandatories');

         $this->validate($request, [

        ]);

}

问题模型:

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

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

这个dd($ mandatories);或者dd($ values);总是,独立于所选的checobx,返回:

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

“dd($ id);”返回会议ID:

"1"
php laravel
1个回答
0
投票

您不需要该模型,您可以使用同步保存它和其他数据

假设$id是问题ID

$question = Question::find($id);

$relationSync = array_merge(
  array_fill_keys($mandatories,["required" => true]),
  array_fill_keys($values,["required" => false])
);

$question->registration_type()->sync($relationSync);

来源文档:https://laravel.com/docs/5.6/eloquent-relationships#updating-many-to-many-relationships

© www.soinside.com 2019 - 2024. All rights reserved.