Randeep Singh Randeep Singh - 6 months ago 19
SQL Question

PHP Laravel : Ajax couldn't retreive data (No properties)

I'm building a laravel application where I want to generate some report with ajax controller. With the same raw query in MySQL I can see the data but using ajax I couldn't show in my view page.
in console it's showing

[] No Properties


enter image description here

I'm not getting why it's showing the above Error.If anyone find what's the wrong, please help me to find it out.Thanks.

Here is the controller I have used for retrieving the data from database:

public function ajax_view_schedule(Request $request)
{

$dept_id= $request->Input(['dept_id']);
$schedule= DB::select(DB::raw("SELECT courses.code as c_code, courses.name as c_name,COALESCE( CONCAT('R. No',':',rooms.room_number,', ',days.name ,', ', allocate_rooms.start,' - ',allocate_rooms.end),'Not Scheduled Yet') AS schedule
FROM departments join courses on departments.id = courses.department_id
left join allocate_rooms on allocate_rooms.course_id=courses.id
left join rooms on allocate_rooms.room_id=rooms.id
left join days on allocate_rooms.day_id=days.id WHERE departments.id='.$dept_id.'"));
return \Response::json($schedule);
}


And here is the view page with ajax code:

<div class="container" >
<h3> View Class Schedule and Room Allocation Information </h3>

<div class="form-group">
<label for="">Department</label>
<select class="form-control input-sm" required id="department" name="department_id" >
<option>Select a Department</option>
@foreach($department as $row)
<option value="{{$row->id}}">{{$row->name}}</option>
@endforeach
</select>
</div>



<table class="table table-striped table-bordered" id="example">
<thead>
<tr>

<td>Course Code</td>
<td>Name</td>
<td>Schedule Info</td>
</tr>
</thead>
<tbody>

</tbody>
</table>
</div>

<script type="text/javascript">
$('#department').on('change',function(e){
var dept_id = $('#department option:selected').attr('value');

$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});

$.ajax({
type: "POST",
url : "{{url('ajax-view-schedule')}}",
data:{dept_id:dept_id},
success : function(data) {
var $tbody = $('#example tbody').empty();
$.each(data,function(index,subcatObj){
$tbody.append('<tr><td class="code">' + subcatObj.c_code + '</td><td class="course_name">' + subcatObj.c_name + '</td><td class="schedule">' + subcatObj.schedule + '</td></tr>');
});
}
});
});
</script>

Answer

You are actually in double quotes so php will interpolate your variable

  public function ajax_view_schedule(Request $request)
    {

          $dept_id= $request->Input(['dept_id']);
$schedule= DB::select(DB::raw("SELECT courses.code as c_code, courses.name as c_name,COALESCE( CONCAT('R. No',':',rooms.room_number,', ',days.name ,', ', allocate_rooms.start,' - ',allocate_rooms.end),'Not Scheduled Yet') AS schedule
FROM departments join courses on departments.id = courses.department_id
left join allocate_rooms on allocate_rooms.course_id=courses.id 
left join rooms on allocate_rooms.room_id=rooms.id
left join days on allocate_rooms.day_id=days.id WHERE departments.id='$dept_id'"));//remove dot from .$dept_id.
        return \Response::json($schedule);  
    }