简体   繁体   中英

Cannot access id being passed in vue.js and laravel

I am having hard time inserting the values which is the id that is being passed from the form into the controller of my enrollment system. To achieve this I make use of vue.js and vue-resource library. In my all.js file I am gettting the correct id value when I console.log the id being passed. However, when passing it to the route address of my application I am getting 500 error. Also, upon debugging I' m getting this error SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'section_subject_id' cannot be null (SQL: insert into reservations . How can I solve this?

ReservationController.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>

Try formatting the data being sent like this: {id: this.id}

So your Vue addSubject Method looks like this:

    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');
        });
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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