title: Submitting a post request through Vuejs and axios to a laravel back-end
body: <p>I need some help in implementing a basic ajax request through vue to my laravel back-end, I have a boolean named completed on a table called courses, and I have a view that fetches all courses assigned to a specific user and allows them to press a button to change the current status of the course, either completed or not, that's it, that's all I wanna do, right now I can do it normally through get and post requests, obviously results in a refresh of the page, I want that to be dynamic with just refreshing the dom, I am so frustrated that I couldn't figure this out on my own because I think It should be easy, turns out I know nothing when it comes to using vuejs.</p>
  
  <p>Here is the significant part of my CoursesController:</p>
  
  <pre><code>  public function toggling($name)
        {
          $course = Course::where(['name' =&gt; $name])-&gt;first();
  
          $course-&gt;completed = !$course-&gt;completed;
          $course-&gt;save();
  
          // return redirect()-&gt;back();
          return response()-&gt;json(['course' =&gt; $course], 202);
          }
  </code></pre>
  
  <p>And here is the significant part of the view that provides the courses to the user, it's a part of a table:</p>
  
  <pre><code>    &lt;td&gt;
       &lt;form method="POST" @submit.prevent="onSubmit" action="{{ route('course.completed', $course-&gt;name) }}" id="form-submit"&gt;
           {{-- {{ method_field('PUT') }} --}}
          {{ csrf_field() }}
         @if ($course-&gt;completed == true)
         &lt;button type="submit" class="btn btn-sm" id="coursetogglingtrue"&gt;Done!&lt;/button&gt;
          @else
        &lt;button type="submit" class="btn btn-sm" id="coursetogglingfalse"&gt;Not Yet!&lt;/button&gt;
          @endif
  
        &lt;/form&gt;
     &lt;/td&gt;
  </code></pre>
  
  <p>For some reason the @submit.prevent method is not working, it worked a couple of times but then It just didn't, and the form kept submitting as usual.</p>
  
  <p>These are the scripts inside app.blade.php, I don't know how/where should I compile this, it's just sitting there in the main layout of my project, should I transfer it to public/js/app.js? or compile it using gulp? if you have something in mind please let me know:</p>
  
  <pre><code>&lt;!-- Scripts --&gt;
  &lt;script src="https://unpkg.com/axios/dist/axios.min.js"&gt;&lt;/script&gt;
  &lt;script src="https://unpkg.com/vue@2.1.10/dist/vue.js"&gt;&lt;/script&gt;
  &lt;script src="/js/app.js"&gt;&lt;/script&gt;
  &lt;script&gt;
    new Vue({
        el: '#app',
        data: {
          course: {}
        },
        methods: {
          onSubmit() {
            // course = $event.target;
            axios.post('/MyCourses/{{course.name}}').then(console.log(course));
          }
        }
    });
  &lt;/script&gt;
  </code></pre>
  
  <p>I want to have course be equal to whatever value of the request was, so that I can then target the value of the name of the course that was submitted and inject it to the route as I'm trying to do, but I'm not sure how to do that, I tried a few different things, all failed.
  And here is routes/api.php</p>
  
  <pre><code>Route::post('/MyCourses/{name}', 'CoursesController@toggling')-&gt;name('course.completed');
  </code></pre>
  
  <p>ofc right now this is not working, the form is submitting, and I'm redirected to a route where I get back the json response, and that's it, I just want to refresh the dom so that the button would have the new id depending on that status of the course, and the course itself to be updated and saved in the background without refreshing the page.
  I am fairly new to this, I understand that the form should probably be re-written, and I know I've done a lot of mistakes, but I just want it to work, so help would be very appreciated.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>After a lot of hours of trial and error, I'm done with the ajax part and I'm almost there with the rest I just need to do a few things, this is what I've managed to do so far,</p>
  
  <p>In the view:</p>
  
  <pre><code>  &lt;form method="POST" @click.prevent="onSubmit" action="{{ route('course.completed', $course-&gt;name) }}" id="form-submit"&gt;
                         {{ csrf_field() }}
       @if ($course-&gt;completed == true)
       &lt;button type="button" class="btn btn-sm" id="coursetogglingtrue"&gt;Done!&lt;/button&gt;
       @else
       &lt;button type="button" class="btn btn-sm" id="coursetogglingfalse"&gt;Not Yet!&lt;/button&gt;
       @endif
  </code></pre>
  
  <p>Changing the type of the button from submit to button allowed the .prevent method to actually fire, now the form doesn't submit anymore, the form still needs work in order to output the proper class depending on the status of the course but regardless,</p>
  
  <p>This is the script that I have now:</p>
  
  <pre><code>&lt;script&gt;
    new Vue({
        el: '#app',
        data: {
          course: {
            name: '',
            bool: false
          }
        },
        methods: {
          onSubmit: function() {
            this.course.bool = !this.course.bool,
            axios.post('/MyCourses/{{$course-&gt;name}}')
                 .then(function (response){
                    // {{$course-&gt;completed}} = response.course.completed;
                 });
          }
        }
    });
  &lt;/script&gt;
  </code></pre>
  
  <p>Somehow right now, I'm actually sending the post request to the correct route, but there's a problem which has the highest priority right now, $course is referencing the latest course that was added to the page, and not the course that I chose by pressing on the button, now whichever button I press, the last course gets injected to $course, and then ofc to the route in axois, I don't know how to figure that out yet, but so far that course gets updated, and if I inspected the network tab in chrome, I see the response and that value of the completed column gets updated, I believe that there are still some mistakes in the code, but I'll keep trying, if you can point out some more things please let me know, thanks</p>