Menampilkan detail data berdasarkan id pada ajax laravel versi 6

halo, saya ingin mengedit data pada database dengan ajax. menampilkan seluruh isi database dan memilih id sudah bisa. masalahnya tidak bisa terkirim ke controller, ada sarankah? sudah 3 hari tidak ketemu solusinya.

ini syntax table-ajax.js nya:

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

    //create product clicked
    $('#createNewProduct').click(function () {
        $('#saveBtn').val("create-product");
        $('#admin_id').val('');
        $('#adminForm').trigger("reset");
        $('#modelHeading').html("Create New Product");
        $('#ajaxModel').modal('show');
    });

    //edit button clicked
    $('body').on('click', '#editAdmin', function () {
        var id = $(this).data('id');
        console.log(id);
        $.get("{{ url('admin') }}"+"/"+id+"/edit", function (data) {
        $('#modelHeading').html("Edit User");
        $('#saveBtn').val("edit-user");
        $('#ajaxModel').modal('show');
        $('#admin_id').val(data.id);
        $('#name').val(data.name);
        $('#email').val(data.email);
        $('#nim').val(data.nim);
        $('#role').val(data.role);
        })
    })
})

ini syntax route webnya:

Route::get('/', function () {
    return view('auth.login');
});

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');
// Route::get('admin/index', 'AdminController@index')->name('admin.index')->middleware('is_admin');
Route::resource('admin', 'AdminController')->middleware('is_admin');

ini syntax indexnya:

@extends('layouts.main')
@extends('layouts.navbar')
@section('content')
<!-- Begin Page Content -->
<div class="container-fluid">

    <!-- Page Heading -->
    <div class="d-sm-flex align-items-center justify-content-between mb-4">
      <h1 class="h3 mb-0 text-gray-800">Dashboard Admin</h1>
      <a class="btn btn-success" href="javascript:void(0)" id="createNewProduct"> Create New Product</a>
    </div>
    <!-- Content Row -->
    <div class="row">
      <table class="table table-hover">
        <thead>
          <tr>
            <th scope="col">#</th>
            <th scope="col">Name</th>
            <th scope="col">Email</th>
            <th scope="col">NIM</th>
            <th scope="col">Action</th>
          </tr>
        </thead>
        <tbody>
          @foreach ($admin as $admin)
          <tr>
            <th scope="row">{{ $loop->iteration }} </th>
            <td>{{ $admin->name }} </td>
            <td>{{ $admin->email }} </td>
            <td>{{ $admin->nim }} </td>
            <td colspan="2">
              <a href="javascript:void(0)" id="editAdmin" data-id="{{ $admin->id }}" data-toggle="tooltip" class="badge badge-primary">Edit</a>
              <a href="javascript:void(0)" id="deleteAdmin" data-id="{{ $admin->id }}"data-toggle="tooltip" class="badge badge-danger">Delete</a>
            </td>
          </tr>
          @endforeach
        </tbody>
      </table>
</div>
</div>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.0/jquery.validate.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>

    {{-- form --}}
    <div class="modal fade" id="ajaxModel" aria-hidden="true">
      <div class="modal-dialog">
          <div class="modal-content">
              <div class="modal-header">
                  <h4 class="modal-title" id="modelHeading"></h4>
              </div>
              <div class="modal-body">
                  <form id="adminForm" name="adminForm" class="form-horizontal">
                    {{ csrf_field() }}
                     <input type="hidden" name="admin_id" id="admin_id">
                      <div class="form-group">
                          <label for="name" class="col-sm-2 control-label">Name</label>
                          <div class="col-sm-12">
                              <input type="text" class="form-control" id="name" name="name" placeholder="Enter Name" value="" maxlength="50" required="">
                          </div>
                      </div>

                      <div class="form-group">
                          <label class="col-sm-2 control-label">Email</label>
                          <div class="col-sm-12">
                              <input type ="text" id="email" name="email" required="" placeholder="Enter Email" class="form-control"></input>
                          </div>
                      </div>

                      <div class="form-group">
                        <label class="col-sm-2 control-Label">NIM</label>
                        <div class="col-sm-12">
                          <input type="text" id="nim" name="nim" required="" placeholder="Enter NIM" class="form-control"></input>
                      </div>
                      </div>

                      <div class="form-group">
                        <label class="col-sm-2 control-Label">Role</label>
                        <div class="col-sm-12">
                          <input type="text" id="role" name="role" required="" placeholder="" class="form-control"></input>
                      </div>
                      </div>

                      <div class="col-sm-offset-2 col-sm-10">
                       <button type="submit" class="btn btn-primary" id="saveBtn" value="create">Save changes
                       </button>

                      </div>
                  </form>
              </div>
          </div>
      </div>
      <script src="{{ asset('/js/table-ajax.js') }} " type="text/javascript">
// end off main content
@endsection

ini syntax controllernya:

<?php

namespace App\Http\Controllers;

use App\Admin;
use Illuminate\Http\Request;
use DataTables;

class AdminController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request, Admin $admin)
    {

        // # code...
        $admin = Admin::orderBy('id', 'ASC')->get();
        // $user = User::orderBy('id', 'DESC')->get();
        if ($request->ajax()) {
            $admin = Admin::orderBy('id', 'ASC')->get();
        }
        return view('admin.index', compact('admin'));
        // return view('admin.index', compact('user'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Admin  $admin
     * @return \Illuminate\Http\Response
     */
    public function show(Admin $admin)
    {
        //
    }


    public function edit(Admin $admin, Request $request)
    {
        if ($request->ajax()) {
            $where = array('id'->$admin->id);
            $admin = Admin::where($where)->first();
        }
        // $admin = Admin::find($admin->id);
        return response()->json($admin);
    }


    public function update(Request $request, Admin $admin)
    {
        //
    }
    public function destroy(Admin $admin)
    {
        //
    }
}

tolong solusinya.. udah 3 hari nyari tapi belom ketemu

terimakasih

avatar alwialdi
@alwialdi

1 Kontribusi 0 Poin

Diperbarui 4 tahun yang lalu

1 Jawaban:

Jawaban Terpilih

Karena url pada form belum di setup,,,

avatar Rismal
@Rismal

131 Kontribusi 105 Poin

Dipost 4 tahun yang lalu

Tanggapan

di file mananya ya kak?

di file 'table-ajax.js' dan untuk tombol edit yang dalam looping atau foreach jangan gunakan id tp class

terimakasih kak.. maaf baru respon.. baru nyentuh projectnya lagi

Login untuk ikut Jawaban