Home > OS >  Illuminate\Database\QueryException SQLSTATE[23000]: Integrity constraint violation: 1048 Column &#
Illuminate\Database\QueryException SQLSTATE[23000]: Integrity constraint violation: 1048 Column &#

Time:08-07

I have been trying to insert data in MySQL DB but I am getting a null value in my CNIC column, I don't why it's a getting null value even though I inserted the data in this column.These are the values (100001, Anas, Mehmood, ?, 03127181279, MALE, 25, 1996-10-29, Bahawalpur, bwp, ?, M, A POSITIVE, CCB, SINGLE, ?, ?, 2022-08-06 04:51:40, 2022-08-06 04:51:40)) i am getting when i insert the data ,in these value after anas mehmood there is a question mark (?) thats the cnic value idk why its giving null

** 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;

class Helpercontroller extends Controller
{
   
    function save(Request $request){
   
$fname= $request->fname;
$lname= $request->lname;
$cnic= $request->cnic;
$contactno= $request->contactno;
$gender= $request->gender;
$age= $request->age;
$dob= $request->dob;
$city= $request->city;
$address= $request->address;
$husbandname= $request->husbandname;
$fathername= $request->fathername;
$bloodgroup= $request->bloodgroup;
$primaryphy= $request->primaryphy;
$clientname= $request->clientname;
$maritalstatus= $request->maritalstatus;
$emergencyno= $request->emergencyno;
$category= $request->category;



$data = DB::table('patients')->where('cnic',$cnic)->count();
if($data == 0)

      {
        $patientid = IdGenerator::generate(['table' => 'patients', 'length' => 6, 'prefix' =>'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->clientname=$clientname;
        $query->maritalstatus=$maritalstatus;
        $query->emergencyno=$emergencyno;
        $query->category=$category;

$query->save();
        
      }
else{

    return back()->with ('fail','Patient already exists with the same CNIC Number');

    
}


if ($query){
    return back()->with ('success',$patientid );
    
}
else{
    return back()->with ('fail','something went wrong');
}

}




public function search()
    {
        return view ('registration');
    }



function searchpatient(Request $request){
    $patientid = $request->input('patientid');
    


    $data = DB::table('patients')->where(['patientid'=>$patientid])->first();

    return  $data;
}

function searchpatientcnic(Request $request){
    $cnic = $request->input('cnic');
    


    $data = DB::table('patients')->where(['cnic'=>$cnic])->first();

    return  $data;
}
    
        
    }
    

    


** This is my web route: **

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\login;
use App\Http\Controllers\MainController;
use App\Http\Controllers\add1;
use Illuminate\Http\Request;
use App\Http\Controllers\Helpercontroller;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/registration',[Helpercontroller::class,'search']);


Route::get('/searchpatient' , [Helpercontroller::class,'searchpatient']);
Route::get('/searchpatientcnic' , [Helpercontroller::class,'searchpatientcnic']);

** This is my blade file: **

<div >
    <div >
        <div >

        <div  style="margin-top: 50px">

    <h4>Patient Registration</h4>
</div><br>

        @if(Session::get('success'))
<div >
    {{Session::get('success')}}
</div>
@endif  

@if(Session::get('fail'))
<div >
{{Session::get('fail')}}
</div>
@endif

    <div >
        <div >
            <div >
            <div >
            <form action="save" method="post" autocomplete="off">
            @csrf
            <label>First Name:<span >*</span></label>
            <input placeholder="" autofocus type="text" name="fname"  tabIndex="2" id="fname" value="{{old('fname')}}" required >
                </div>
                

                <div >
                <label>Last Name:</label>
            <input placeholder="" type="text" name="lname"  tabIndex="3" id="lname"  value="{{old('lname')}}" >
                </div>
                


                <div >
                <label>Age:</label>
            <input placeholder="" type="text" name="age"  tabIndex="4" id="age"  value="{{old('age')}}"  >
                </div>
            



                
                <div >
                <label>DOB:</label>
            <input placeholder="" type="date" name="dob"  tabIndex="5" id="dob"  value="{{old('dob')}}"  >
                </div>
                

                
                <div >
                <label>Gender:<span >*</span></label>
                
          <select  name="gender" tabIndex="6" id="gender" value="{{old('gender')}}" required >
          <option value="">SELECT</option>
        <option value="MALE">MALE</option>
        <option value="FEMALE">FEMALE</option>
        <option value="NEUTER">NEUTER</option>
           </select>
                </div>
                



                <div >
                <label>Contact No:<span >*</span></label>
            <input placeholder="" type="number" name="contactno"  tabIndex="7" id="contactno" data-validate="Valid Contact# is required" pattern="^\d{11}$"  tabIndex="6"  value="{{old('contact')}}" required >
                </div>
                

                <div >
                <label>Father Name:</label>
            <input placeholder="" type="text" name="fathername"  tabIndex="8" id="fathername" tabIndex="7"  value="{{old('fathername')}}"  >
                </div>
                


                <div >
                <label>Husband Name:</label>
            <input placeholder="" type="text" name="husbandname"  id="husbandname"  tabIndex="9"  value="{{old('husbandname')}}"  >
                </div>
                



                <div >
                <label>CNIC: <span >*</span></label>
            <input placeholder="" type="number" name="cnic"  id="cnic" tabIndex="10"   pattern="^\d{13}$"  required >
                </div>
                    

                <div >
                <label>City: <span >*</span></label>
            <input placeholder="" type="text" name="city"  id="city"  tabIndex="11"  value="{{old('city')}}" required >
                </div>
                


                <div >
                <label>Category:</label>
            <input placeholder="" type="text" name="category"  id="category"  tabIndex="12"  value="{{old('category')}}" >
                </div>
                



                <div >
                <label>Emergency No:</label>
            <input placeholder="" type="number" name="emergencyno"  id="emergencyno" tabIndex="13"  value="{{old('emergencyno')}}"  >
                </div>
                


                <div >
                <label>Marital Status:</label>
                <select  name="maritalstatus" tabIndex="14" id="maritalstatus" value="{{old('maritalstatus')}}"  >
                <option value="">SELECT</option>
        <option value="NOT SPECIFIED">NOT SPECIFIED</option>
        <option value="SINGLE">SINGLE</option>
        <option value="MARRIED">MARRIED</option>
        <option value="WIDOW">WIDOW</option>
        <option value="WIDOWER">WIDOWER</option>
        <option value="DIVORCED">DIVORCED</option>
        <option value="SEPARATED">SEPARATED</option>
        <option value="UNKNOWN">UNKNOWN</option>
           </select>
                </div>
                

                

                <div >
                <label>Blood Group:</label>
              <select  name="bloodgroup" tabIndex="15" id="bloodgroup"  value="{{old('bloodgroup')}}"  >
          <option value="">SELECT</option>
        <option value="NOT SPECIFIED">NOT SPECIFIED</option>
        <option value="A POSITIVE">A POSITIVE</option>
        <option value="A NEGATIVE">A NEGATIVE</option>
        <option value="B POSITIVE">B POSITIVE</option>
        <option value="B NEGATIVE">B NEGATIVE</option>
        <option value="O POSITIVE">O POSITIVE</option>
        <option value="O NEGATIVE">O NEGATIVE</option>
        <option value="AB POSITIVE">AB POSITIVE</option>
        <option value="AB NEGATIVE">AB NEGATIVE</option>
           </select>
                </div>
                
               

                <div >
                <label>Primary Phy:</label>
            <input placeholder="" type="text" name="primaryphy"  id="primaryphy"  tabIndex="16"  value="{{old('primaryphy')}}"  >
                </div>
                


              

                <div >
                <label>Address:</label>
            <input placeholder="" type="text" name="address"   id="address" tabIndex="17"  value="{{old('address')}}"  >
                </div>
               

              

                <div >
                <label>Client Name:</label>
              <select   name="clientname" tabIndex="18" id="clientname" value="{{old('clientname')}}"  >
          <option value="">SELECT</option>
        <option value="SPECIAL FINANCIAL APPROVAL">SPECIAL FINANCIAL APPROVAL</option>
        <option value="SEHAT CARD">SEHAT CARD</option>
        <option value="CCB">CCB</option>
        <option value="BVH HOSPITAL">BVH HOSPITAL</option>
        <option value="CCB HOSPITAL/INDOOR">CCB HOSPITAL/INDOOR</option>
        <option value="BH-MLC">BH-MLC</option>
           </select>        
        </div>
        
                
                <div >
                <label>Patient ID:</label>
            <input placeholder="" type="number" name="patientid" id="patientid"  disabled >
                </div>


        
                


                <div >
                <button type="submit"  value="" tabIndex="19">Register</button>
                    
                        </div>

                        <div >
                        <button type="submit"  value=""  >Update</button>
                        </div>

                        
                        <div >
                        <button type="submit"  value="" >Refresh</button>
                        </div>

                        <div >
                        <button type="submit"  value=""  >Clear</button>
                        </div>
                        <div >
                        <button type="submit" >Update Contact</button>
                            
                        </div>
                        <div >
                        <button type="submit" >Update Info</button>
                        </div>

                        <div >
        
                <label>Search Patient :</label>
            <input placeholder="Enter Patient ID" type="number"  name="patientid" id="selectpatientid"   >
              <input placeholder="Enter Patient CNIC" type="number"  name="cnic" id="selectpatientcnic"   >

                </div>  

</form>

</div>
</div>
</div>
</div>
</div>
</div>
</div>

CodePudding user response:

  1. may cnic not in $fillable in your model. add to fillable like: protected $fillable = ['all columns', 'cnic']
  2. check spell of variable names in blade and controller.
  3. try run your route with postman. just write your route and send the request data and send it if work, it means the problem in your blade file, if not work the problem in controller.
  • Related