mohammd mohammd - 27 days ago 13
Ajax Question

ajax with laravel return undefined when i echo the id but when i echo the name it works

so i have testgroups and projects testgroup has the id of project so i can get the testgroups that has this projects and show them in my page it only echo the names but the id dont show it shows as undefined heres my ajax script

$("#projects").change(function(){

var id = Number($(this).children(":selected").val());
$("#testgroup").html('');

$.get("/management/get-test-group/"+id, function(data, status){
if(status == "success")
{

var loopdata = JSON.parse(data);

$.each(loopdata, function(i, item) {

$('#testgroup').append("<option value='" + loopdata[i].testgroupid + "'>" + loopdata[i].TestGroupName + "</option><br>");

});
if (loopdata=='')
{
$('#testgroup').append("<option>" + 'No Test Groups' + "</option>");

}

}
});
});


and heres the controller

public function GetTestGroup($id)
{
$testcase = testgroup::where('project_id',$id)->get();

if($testcase)
{
return json_encode($testcase);
}
}


and my view i want to echo the name and the id but it only gets the name and the id shows as undefined

<div class="form-group">
<label> Projects </label>
<select class="form-control" name="project" id="projects">
@foreach($projects AS $project)
<option value="{{$project->id}}">
{{$project->ProjectName}}
</option>
@endforeach
</select>
</div>
<div class="form-group">
<label > Test Group </label>
<select name="testgroup" class="form-control" id="testgroup" >

</select>
</div>

Answer Source

You need to open your model file and remove:

$hidden = [ 'testgroupid'];

After that check if it works.