0

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>

1 Answer 1

1

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');
        });
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.