mardi 26 mars 2019

How to get checkbox checked from database array using Laravel and Ajax

I'm trying to get data from database to checkboxes as checked. The checkbox data in database are as array that have been inserted with json and they are dynamic data based on insert function.

My tasks table:

id |employee_id | startDate  | endDate   | ...
---|------------|------------|-----------|--------
1  |["1","2"]   | .......... | ..........| ....

My TasksController.php

function fetchdata(Request $request)
    {
        $id = $request->input('id');
        $task = Task::find($id);
        $output = array(
            'employee_id'    =>  $task->employee_id,
            'name'    =>  $task->name,
            'description'    =>  $task->description,
            'startDate'    =>  $task->startDate,
            'endDate'    =>  $task->endDate,
            'percentage'    =>  $task->percentage       

        );
        echo json_encode($output);
    }

    public function getEmployeesList()
    {

        $employeesList = Employee::all();


        return view('adminlte::tasks', ['employeesList' => $employeesList]);


    }

My tasks.blade.php

 @foreach($employeesList as $emplist)
    <label class="checkbox-inline">
      <input type="checkbox" id="employee_id" name="employee_id[]" class="chck" value="" ></label>    
 @endforeach


My Ajax function inside blade:

$(document).on('click', '.edit', function(){
        var id = $(this).attr("id");
        $('#form_output').html('');
        $.ajax({
            url: "",
            method: 'get',
            data: {id:id},
            dataType: 'json',
            success:function(data)
            {
                $('#id').val(data.id);
                $('#employee_id').val(data.employee_id);
                $('#name').val(data.name);
                .......................
                ..................
            }
        })
    });

So, how can I retrieve data from database to checkboxes as checked, because for now I'm getting null "employee_id" value when I try to update a record.

Thank you in advance




Aucun commentaire:

Enregistrer un commentaire