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, help would be appreciated.
Here is the significant part of my CoursesController:
public function toggling($name)
{
$course = Course::where(['name' => $name])->first();
$course->completed = !$course->completed;
$course->save();
// return redirect()->back();
return response()->json(['course' => $course], 202);
}
And here is the significant part of the view that provides the courses to the user, it's a part of a table:
<td>
<form method="POST" action="{{ route('course.completed', $course->name) }}" id="form-submit">
{{ csrf_field() }}
@if ($course->completed == true)
<button type="submit" class="btn btn-sm" id="coursetogglingtrue">Done!</button>
@else
<button type="submit" class="btn btn-sm" id="coursetogglingfalse">Not Yet!</button>
@endif
</form>
</td>
I think I should prevent the default action of the form, and I remember that vuejs had a .prevent or something similar but I just can't remember/find it, I think this whole form should be re-written to apply the new logic where it depends on vuejs.
routes/web.php, right now I just commented out that route and copied it over to api.php:
Route::get('/MyCourses', 'CoursesController@myCourses')->name('student.mycourses');
// Route::post('/MyCourses/{name}', 'CoursesController@toggling')->name('course.completed'); //Changing The Completed Status On The Courses Table
routes/api.php:
Route::post('/MyCourses/{name}', 'CoursesController@toggling')->name('course.completed');
For that response()->json() thing, I actually got that from a video doing mostly the same thing, but then I didn't know what to do with it, when the route is triggered I get redirect to api/Mycourses/{name} and the course object would be displayed in json, I'm not really sure what I should do at this point, but what I want to do is when the user clicks on that button the other id gets applied, and then an ajax request would be sent so that the value of the completed column for that course would be updated depending on the current state of the button, just like what's happening now, just without refreshing the dom, I know I should probably use something called axios but I didn't really understand how does it work, I also know I should work on my skills cause I have probably done a lot of mistakes, feel free to edit the whole thing if it's gonna work at the end, thanks for your time.