简体   繁体   English

插入选择选项表Laravel-Vue

[英]Insert Select Option Form Laravel - Vue

I wanna input data to database from Select Option Form. 我想从“选择选项表单”中将数据输入数据库。 I used Laravel and Vue. 我使用了Laravel和Vue。 This is my Form 这是我的表格

<div class="form-group">
   <label for="exampleFormControlSelect1">Pemilihan*</label>
   <select class="form-control" id="electionId" name="electionId" v-model="electionIdSelected">
     <option v-for="option in electionIdOptions" v-bind:value="option.value">{{option.text}}</option>
   </select>
</div>

This is my Vue: 这是我的Vue:

data() {
          return{
                 electionIdSelected:'Please choose one',
                 electionIdOptions:[
                   {text: 'Presiden', value: 'ppwp'},
                   {text: 'DPRD Provinsi', value: 'pdpr'}
                 ],
           methods: {
            onSubmit() {
                axios.post('/psu/list/store', {
                   electionIdSelected: this.electionIdSelected,
                }).then(response => {
                   this.electionIdSelected = ''
             });
            }

This is my Controller in Laravel: 这是我在Laravel中的控制器:

public function store(){
      $psu = new Psu;

      $psu->jenis_pemilihan = request('electionId');

      $psu->save();
    }

Please help me to insert form Select option into database. 请帮助我在数据库中插入表格选择选项。 I still got error. 我仍然有错误。 This is an Error Message: 这是一条错误消息:

null value in column "jenis_pemilihan" violates not-null constraint

Cant get value to Controller and Model. 不能为控制器和模型获取价值。

public function store(Request $request){
  $psu = new Psu;

  $psu->jenis_pemilihan = $request->input('electionIdSelected');

  $psu->save();
}

I think electionId is empty in your controller because you never set such a key in the axios post ? 我认为您的控制器中的lectionId为空,因为您从未在axios帖子中设置过这样的键吗?

You can try to send electionIdSelected.text for that your selectbox model is electionIdSelected when u set your option.text to the model then your values look like electionIdSelected.text 当您将option.text设置为模型时,您可以尝试发送选择框模型为lectionIdSelected的electionIdSelected.text,然后您的值看起来就像electionIdSelected.text

Just replace this line : electionIdSelected: this.electionIdSelected to electionIdSelected: this.electionIdSelected.text 只需替换此行: electionIdSelected: this.electionIdSelectedelectionIdSelected: this.electionIdSelected.text

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM