Check your laravel log. 500 sounds like an error with your PHP.
Using $Ajax with Laravel
Hi guys ! I created a nested list with data from database. Also I'm using JQuery/JQuery UI to create a Drag/Drop event on each element of the list. But I keep getting:
500 (Internal Server Error)
The main idea is: I have a bunch of professors and each professor may have 0 or N classes to teach, lets pretend that:
John | Math | Physics
Dwayne | English
Now, lets say I want Dwayne to teach Math instead of John. SO I'll drag the math from John list and drop it on Dwayne. In the moment I drop the new element, I want to fire an ajax function to update my database:
My Code so far:
Routes
Route::get('professor', [
'uses' => 'ProfessorController@index'
]);
Route::post('professor', [
'uses' => 'ProfessorController@postProfessorList'
]);
Controller
class ProfessorController extends Controller
{
public function index()
{
return View::make('professor', [
'professores' => Professor::all()
]);
}
public function postProfessorList()
{
Professor::submit(Input::post('disciplina'), input::post('professor'), input::post('old'));
}
}
My View
@extends('app')
@section('assets')
<script type="text/javascript" src="{{ URL::to('/js/jquery.mjs.nestedSortable.js') }}"></script>
@stop
@section('content')
<ol>
@foreach($professores as $prof)
<li data-id=" {{ $prof->id }}">
{{ $prof->nome }}
<ol class="list-disc">
@foreach($prof->disc as $disc)
<li data-id="{{ $disc->id }}">{{ $disc->nome }}</li>
@endforeach
</ol>
</li>
@endforeach
</ol>
<script type="text/javascript">
$(function(){
var old_teacher;
$('.list-disc').sortable({
connectWith: '.list-disc',
start: function (event, ui){
old_teacher = ui.item.parent().parent().attr('data-id');
dd($old_teacher)
},
stop: function (event, ui){
$.ajax({
type: "POST",
url: '{{ URL::to("/professor") }}',
data: {disc: ui.item.attr('data-id'), professor: ui.item.parent().parent().attr('data-id'), old: old_teacher},
success: function(data){
//console.log(data);
}
});
}
});
})
</script>
@stop
Seemingly, you fixed your csrf_token error (You HAD a mismatch error at one point). You're hitting your controller correctly now. You could simply return the whole request/input as a response and use chrome dev's network tools to make sure everything is received properly.
Now you're getting an Undefined method called because the Input class (an alias for Request) does not have a post() method. So Input::post() (or Request::post()) doesn't exist. It should be Input::get() (or Request::get())
Lastly, your using a query scope, and incorrectly, which will likely cause you to run into another error once your Undefined method called error is resolved.
Please or to participate in this conversation.