Mohammad hayajneh
Mohammad hayajneh

Reputation: 674

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>

Upvotes: 1

Views: 340

Answers (2)

Giorgi Khurtsidze
Giorgi Khurtsidze

Reputation: 104

You need to open your model file and remove:

$hidden = [ 'testgroupid'];

After that check if it works.

Upvotes: 2

Mahdi P.
Mahdi P.

Reputation: 307

check your testgroup model and check if id is not in $hidden array like protected $hidden = ['id']; or any array that prevent id to be shown;

Upvotes: 0

Related Questions