Home > database >  Unable to save data using ajax in Laravel
Unable to save data using ajax in Laravel

Time:10-10

Unable to save data in the database using the ajax function, I have used validation if the record exists it will alert the record already exists, else it will save the data, but here it's only validating the data but not saving the data. I am getting the error $query not defined in the controller. This is my controller

<?php

namespace App\Http\Controllers;

use Haruncpi\LaravelIdGenerator\IdGenerator;

use Illuminate\Http\Request;
use App\Helpers\Helper;
use Illuminate\Support\Facades\DB;
use App\User;
use App\Models\patient;
use Illuminate\Support\Facades\Validator;

class Helpercontroller extends Controller
{

    public function save(Request $request)
{
   $validator = \Validator::make($request->all(), [
       
        'cnic' => 'required|unique:patients',
    ]);
    
    if ($validator->fails()) {
        return response()->json(['success' => false, 'errors' => $validator->errors()->all()]);
    }

    $temppatientId = IdGenerator::generate(['table' => 'patients', 'length' => 5, 'prefix' => '22']);

    $patientid = $temppatientId   1;

    $query = new patient;
    $query->patientid = $patientid;
    $query->fname =$fname;
    $query->lname = $lname;
    $query->cnic = $cnic;
    $query->contactno = $contactno;
    $query->gender = $gender;
    $query->age = $age;
    $query->dob = $dob;
    $query->city = $city;
    $query->address = $address;
    $query->husbandname = $husbandname;
    $query->fathername = $fathername;
    $query->bloodgroup = $bloodgroup;
    $query->maritalstatus = $maritalstatus;
    

    $query->save();
   

    return response()->json(['success' => true, 'patients' => $query]);
}

This is my ajax call

$(document).ready(function() {
    $("#save1").on('click', function(e) {

        var cnic = $("#cnic").val();
        
       
        if (cnic == '') {
            alert("Kindly Enter CNIC");
            return false;
        }

        var gender = $("#gender").val();
        if (gender == '') {
            alert("Kindly Enter Gender");
            return false;
        }
       
    



       
        var contactno = $("#contactno").val();
        if (contactno == '') {
            alert("Kindly Enter Contact No");
            return false;
        }
        



        var fname = $("#fname").val();
        if (fname == '') {
            alert("Kindly Enter Name");
            return false;
        }

else{

    $.ajax({
        url: "/save",
        method: "post",
        data: $('#registrationform').serialize(),
        dataType: 'json',
        success: function(data) {
            if (data.success !== false) {
                $("#patientid").val(data.patients.patientid);
               
            } else {
                alert('CNIC already Exists !');
                
            }
        }
  })

}
        
    });
});

This is my route

Route::post('save', [Helpercontroller::class, 'save'])->name('save');

CodePudding user response:

Variables appear to be undefined.

$query->fname = $request->fname;
  • Related