0

我很难将作为从表单传递的 id 的值插入到我的注册系统的控制器中。为了实现这一点,我使用了 vue.js 和 vue-resource 库。在我的 all.js 文件中,当我 console.log 传递的 id 时,我得到了正确的 id 值。但是,当将它传递给我的应用程序的路由地址时,我收到 500 错误。另外,在调试时,我收到此错误 SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'section_subject_id' cannot be null (SQL: insert into reservations。我该如何解决这个问题?

预订控制器.php

class ReservationController extends Controller
{

    public function create($id)
    {

        $subjects = Subject::with('sections')->get();

        return view('reservation.form',compact('subjects'));
    }

    public function store(Request $request)
    {

        $subject = new Reservation();

        $subject->section_subject_id = $request->input('id');

        $subject->student_id = 1;

        $subject->save();

    }
}

all.js

Vue.http.headers.common['X-CSRF-TOKEN'] = document.querySelector('#token').getAttribute('value');

new Vue({
    el: '#app-layout',

    data: {
        subject: {
            id : ''
        },
        subjects: []

    },
    ready: function(){

    },
    methods:{
        addSubject: function(id){

            this.subject = id;

            this.$http({url: 'http://localhost:8000/reservation', id, method: 'POST'}).then(function(response){

            }, function (response){
                console.log('error');
            });
        }
    }
});

form.blade.php

<body>
  @foreach($subjects as $subject)
  @foreach($subject->sections as $section)
  <tr>
    <td>{{ $section->section_code }}</td>
    <td>{{ $subject->subject_code }}</td>
    <td>{{ $subject->subject_description }}</td>
    <td>{{ $section->pivot->schedule }}</td>
    <td>{{ $subject->units }}</td>
    <td>{{ $section->pivot->room_no }}</td>
    <td>
      <button 
         v-on:click="addSubject( {{ $section->pivot->id }} )" 
         class="btn btn-xs btn-primary">Add
         </button>

       <button class="btn btn-xs btn-info">Edit</button>
     </td>
    </tr>
   @endforeach
  @endforeach
 </body>
4

1 回答 1

1

尝试像这样格式化发送的数据:{id: this.id}

所以你的 Vue addSubject 方法看起来像这样:

    addSubject: function(id){

        this.subject = id;

        this.$http({url: 'http://localhost:8000/reservation', {id: this.id}, method: 'POST'}).then(function(response){

        }, function (response){
            console.log('error');
        });
    }
于 2016-05-11T16:26:06.133 回答