Laravel 6 – getting first id in database with ajax

  ajax, jquery, laravel

I’m beginner in jQuery/Ajax. I’m trying to insert data to database with Ajax. I can insert data whatever I want without reload page but when I look at the data with ‘console.log’ I added, I see the id of the first data in the table. Where is my fault?

My blade is:

    <a href="javascript:void(0)" type="button" class="btn btn-success" data-toggle="modal" data-target="#addDoctor" >Add doctor</a>

<div class="modal fade" id="addDoctor" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
        <div class="modal-header">
            <h5 class="modal-title" id="exampleModalLabel">Add</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
        <div class="modal-body">
            <form id="doctorForm" method="POST" action="{{route('add_doctorPost')}}" novalidate>
                <div class="mb-3">
                    <input type="text" class="form-control" name="name" id="name" required>
                <div class="mb-3">
                    <label>E-mail </label>
                    <input type="email" class="form-control" name="email" id="email" required>
                <button type="submit"  class="btn btn-warning btn-lg btn-block">Kaydet</button>

My ajax is:


    let name = $("#name").val();
    let email = $("#email").val();

        url: "{{route('add_doctorPost')}}",
        data:{name:name,email:email,_token:'{{ csrf_token() }}'},
                $("#doctorTable tbody").prepend('<tr><td><p class="c_name">'+ +'</p></td><td><p class="c_name">'+ +'</p></td></tr>')
      "nice!", response.message, "success");


Source: Laravel

Leave a Reply