How to edit data in yajra datatable laravel with data from another table?

Viewed 514

I want to make an edit on my laravel6 project.

This is my edit button from EmployeeController@index:

public function index(Request $request)
    {
        if (!Auth::check()) {
            return redirect()->route('login');
        }

        if ($request->ajax()) {
            // ambil semua data
            $employees = Employee::all();
            return Datatables::of($employees)
                ->addIndexColumn()
                ->addColumn('action', function () {
                    $btn = '<a href="javascript:void(0)" class="edit btn btn-primary btn-sm">View Detail</a>';

                    return $btn;
                })
                ->addColumn('jenis_kelamin', function ($data) {
                    if ($data->jenis_kelamin == 'M') {
                        return 'Laki - Laki';
                    } else {
                        return 'Perempuan';
                    }
                })
                ->addColumn('data', function ($data) {
                    $button = '<a href="javascript:void(0)" data-toggle="tooltip"  data-id="'.$data->id.'" data-original-title="Edit" class="info badge badge-info btn-sm edit-product">Edit</a>';
   
                    $button .= '&nbsp;&nbsp;&nbsp;<button type="button" name="delete" id="' . $data->id . '" class="delete badge badge-danger btn-sm">Delete</button>';
                    return $button;
                })
                ->rawColumns(['action', 'data'])->make(true);
            // ->rawColumns(['data'])->make(true);
        }

        return view('pages.employee');
    }

Below is a my js datatable for edit:

            // start edit
            /* When click edit user */
            $('body').on('click', '.edit-product', function() {
                var product_id = $(this).data('id');
                $.get('employee/edit/' + product_id, function(data) {
                    $('#productCrudModal').html("Edit Data");
                    $('#btn-save').val("edit-product");
                    $('#ajax-product-modal').modal('show');
                    $('#product_id').val(data.id);
                    $('#nik').val(data.nik);
                    $('#nama_pegawai').val(data.nama_pegawai);
                    $('#jabatan_id').val(data.nama_jabatan);
                })
            });
            // end edit

And below is my code for EmployeeController@edit

public function edit($id)
    {
        $employees = Employee::findOrFail($id);
        return response()->json($employees);
    }

I've done to make a create employee feature, and also I'm done to get the id employee for edit.

How to get the data from another table, when I want to edit using yajra datatable?

I can easily using

$salaries = Sallary::all(); 

when I want to get data from sallaries table for create employee.

But how to pass the salaries data when I make an edit feature?

0 Answers
Related