Illuminate \ Database \ QueryException(23000)SQLSTATE [23000]:违反完整性约束:1048列“ first_name”不能为空

时间:2019-07-30 07:01:10

标签: mysql laravel

我对laravel相当陌生,尝试在数据库中输入值时遇到此问题。同样的事情对于患者的表和表格有效,但不适用于医生。如果有人可以指出我哪里出了问题,那将是不胜感激的

表格

{
    public function up()
    {
        Schema::create('doctors', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('uid')->unique();
            $table->string('first_name');
            $table->string('last_name');
            $table->string('phone');
            $table->string('username')->unique();
            $table->string('email');
            $table->string('password');
            $table->longText('address')->nullable();
            $table->longText('education')->nullable();
            $table->longText('specialist')->nullable();
            $table->date('dob');
            $table->string('blood_group',5);
            $table->enum('sex',['M','F','O']);
            $table->string('nid_file')->nullable();
            $table->string('cv_file')->nullable();
            $table->string('picture')->nullable();
            $table->boolean('status')->default(1);
            $table->unsignedInteger('designation_id');
            $table->unsignedInteger('department_id');
            $table->rememberToken();
            $table->timestamps();
            $table->foreign('designation_id')->references('id')->on('designations')->onDelete('cascade');
            $table->foreign('department_id')->references('id')->on('departments')->onDelete('cascade');
        });

    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('doctors');
    }
}

我要通过其输入值的视图

<div class="card card-tsk mb-4">
    <div class="card-body">
        <form action="{{route('doctor-reg.store')}}" method="post" enctype="multipart/form-data">@csrf
            <div class="form-row">
                <div class="form-group col-md-3">
                    <label for="first_name">First Name <span class="text-danger">*</span></label>
                    <input type="text" class="form-control" id="first_name" name="first_name" placeholder="First Name">
                </div>
                <div class="form-group col-md-3">
                    <label for="last_name">Last Name <span class="text-danger">*</span></label>
                    <input type="text" class="form-control" id="last_name" name="last_name" placeholder="Last Name">
                </div>
                <div class="form-group col-md-3">
                    <label for="username">Username</label>
                    <input type="text" class="form-control" id="username" name="username" placeholder="Username">
                </div>
                <div class="form-group col-md-3">
                    <label for="password">Password</label>
                    <input type="password" class="form-control" id="password" name="password" placeholder="Password">
                </div>
            </div>
            <div class="form-row">
                <div class="form-group col-md-3">
                    <label for="phone">Phone No</label>
                    <input type="text" class="form-control" id="phone" name="phone" placeholder="Phone No">
                </div>
                <div class="form-group col-md-3">
                    <label for="email">Email Address</label>
                    <input type="email" class="form-control" id="email" name="email" placeholder="Email Address">
                </div>
                <div class="form-group col-md-3">
                    {{-- <label for="designation">Designation</label>
                    <select name="designation" id="designation" class="form-control select2">
                        <option value="">Select Designation</option>
                        @foreach($designations as $designation)
                            <option value="{{$designation->id}}">{{$designation->name}}</option>
                        @endforeach
                    </select> --}}
                </div>
                <div class="form-group col-md-3">
                    {{-- <label for="department">Department</label>
                    <select name="department" id="department" class="form-control select2">
                        <option value="">Select Department</option>
                        @foreach($departments as $department)
                        <option value="{{$department->id}}">{{$department->name}}</option>
                            @endforeach
                    </select> --}}
                </div>
            </div>
            <div class="form-row">
                <div class="form-group col-md-6">
                    <label for="address">Address</label>
                    <textarea class="form-control" rows="8" name="address" id="address"></textarea>
                </div>
                <div class="form-group col-md-6">
                    <label for="Education">Education/Degree</label>
                    <textarea class="form-control" name="education" rows="6" id="Education" ></textarea>
                </div>
            </div>
            <div class="form-row">
                <div class="form-group col-md-4">
                    <label for="specialist">Specialist</label>
                    <input type="text" class="form-control" id="specialist" name="specialist" placeholder="Specialist">
                </div>
                <div class="form-group col-md-4">
                    <label for="dob">Date fo Birth</label>
                    <input  class="form-control datepicker" id="dob"  name="dob" value="{{date('Y/m/d')}}" >
                </div>
                <div class="form-group col-md-4">
                    <label for="blood_group">Blood Group</label>
                    <select name="blood_group" id="blood_group" class="form-control select2">
                    <option value="">None</option>
                    <option value="A+">A+</option>
                    <option value="A-">A-</option>
                    <option value="B+">B+</option>
                    <option value="B-">B-</option>
                    <option value="AB+">AB+</option>
                    <option value="AB-">AB-</option>
                    <option value="O-">O-</option>
                    <option value="O+">O+</option>
                    </select>
                </div>
            </div>
            <div class="form-group row">
                <label for="inputAddress2"  class="col-sm-2">Sex</label>
                <div class="col-sm-2">
                    <div class="form-check form-check-inline">
                        <input class="form-check-input" type="radio" name="sex" id="sex1" checked value="M">
                        <label class="form-check-label" for="sex1">Male</label>
                    </div>
                    <div class="form-check form-check-inline">
                        <input class="form-check-input" type="radio" name="sex" id="sex2" value="F">
                        <label class="form-check-label" for="sex2">Female</label>
                    </div>
                </div>


            </div>
            <div class="form-row">
                <div class="form-group col-md-4">
                    <label for="nid_file">Upload NID <span class="help-block">(eg:pdf) </span></label>
                    <input type="file" class="form-control" id="nid_file" name="nid_file">
                </div>
                <div class="form-group col-md-4">
                    <label for="cv_file">Upload Biography <span class="help-block">(eg:pdf) </span></label>
                    <input type="file" class="form-control" id="cv_file" name="cv_file">
                </div>
                <div class="form-group col-md-4">
                    <label for="picture">Picture <span class="help-block">(eg:jpg) </span></label>
                    <input type="file" class="form-control" id="picture" name="picture">
                </div>
            </div>

            <div class="form-group row">
                <label for="inputAddress2"  class="col-sm-2">Status</label>
                <div class="col-sm-2">
                    <div class="form-check form-check-inline">
                        <input class="form-check-input" type="radio" name="status" id="status1" checked value="1">
                        <label class="form-check-label" for="status1">Active</label>
                    </div>
                    <div class="form-check form-check-inline">
                        <input class="form-check-input" type="radio" name="status" id="status2" value="0">
                        <label class="form-check-label" for="status2">Inactive</label>
                    </div>
                </div>


            </div>
            <button type="reset" class="btn btn-outline-tsk"><i class="fa fa-refresh"></i> Reset</button>
            <button type="submit" class="btn btn-tsk"><i class="fa fa-save"></i> Save</button>
        </form>

    </div>
</div>

我对巨大的代码表示歉意,但经过无数次尝试,我仍无法解决此问题,并希望以此方式提供更好的上下文。

控制器

<?php

namespace App\Http\Controllers;

use App\Model\Department;
use App\Model\Designation;
use App\Model\Doctor;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Image;
class DoctorRegister extends Controller
{

    /**
     * @var Doctor
     */
    public $doctor;
    /**
     * @var Department
     */
    public $department;
    /**
     * @var Designation
     */
    public $designation;

    public function __construct(Doctor $doctor,Department $department,Designation $designation)
    {
        $this->doctor = $doctor;
        $this->department = $department;
        $this->designation = $designation;
    }

    public function index(){
        return view('auth.doctor-reg');
    }
    public function create(){
    }
    public function store(Request $request){

     $doctor =new $this->doctor;
     $doctor->first_name = $request->input('fname');
     $doctor->last_name = $request->input('lname');
     $doctor->username = $request->input('username');
     $doctor->password = bcrypt($request->input('password'));
     $doctor->phone = $request->input('phone');
     $doctor->email = $request->input('email');
     $doctor->designations = $request->input('designation');
     $doctor->departments = $request->input('department');
     $doctor->address = $request->input('address');
     $doctor->education = $request->input('education');
     $doctor->specialist = $request->input('specialist');
     $doctor->dob = $request->input('dob');
     $doctor->blood_group = $request->input('blood_group');
     $doctor->sex = $request->input('sex');
     $doctor->plan = $request->input('plan');
     $doctor->cabin = $request->input('cabin');
     $doctor->time = $request->input('time');
     $doctor->day = $request->input('day');
     $doctor->transaction_id = 1;
        if($request->hasFile('nid_file')){
            $nid_file_name = 'nid_'.time().'.pdf';
            $nid_file = $request->file('nid_file');
            $nid_file->move('assets/images/doctor/nid_file',$nid_file_name);
            $doctor->nid_file = $nid_file_name;
        }
        if($request->hasFile('cv_file')){
            $cv_file_name = 'cv_'.time().'.pdf';
            $cv_file = $request->file('cv_file');
            $cv_file->move('assets/images/doctor/cv_file',$cv_file_name);

            $doctor->cv_file = $cv_file_name;
        }
        if($request->hasFile('picture'))
        {
            $picture_path = 'assets/images/doctor/picture/pic_'.time().'.jpg';
            Image::make($request->picture)->resize(200, 200)->save($picture_path);
        }
     $doctor->nid_file = $request->nid_file;
     $doctor->cv_file = $request->cv_file;
     $doctor->picture = $request->picture;
     $doctor->status = $request->status;
     $doctor->save();
     return redirect('/login')->with('success','Doctor has been save successful');
    }

    public function edit($id){
        $doctor =$this->doctor->findOrFail($id);
        $departments = $this->department->orderBy('name')->get();
        $designations = $this->designation->orderBy('name')->get();
        return view('admin.doctor.edit',compact('doctor','departments','designations'));
    }
    public function update(Request $request,$id){

        $this->validate($request,[
            'first_name'=>'required',
            'last_name'=>'required',
            'username'=>'required|unique:doctors,username,'.$id,
            'phone'=>'required',
            'email'=>'required|email',
            'designation'=>'required',
            'department'=>'required',
            'nid_file'=>'mimes:pdf|max:2048',
            'cv_file'=>'mimes:pdf|max:2048',
            'picture'=>'mimes:jpg|max:2048',

        ]);
        $doctor =$this->doctor->findOrFail($id);
        $doctor->first_name = $request->first_name;
        $doctor->last_name = $request->last_name;
        $doctor->username = $request->username;
        $doctor->password = bcrypt($request->password);
        $doctor->phone = $request->phone;
        $doctor->email = $request->email;
        $doctor->designation_id = $request->designation;
        $doctor->department_id = $request->department;
        $doctor->address = $request->address;
        $doctor->education = $request->education;
        $doctor->specialist = $request->specialist;
        $doctor->dob = $request->dob;
        $doctor->blood_group = $request->blood_group;
        $doctor->sex = $request->sex;
        if($request->hasFile('nid_file')){
            @unlink('assets/images/doctor/nid_file/'.$request->nid_file);
            $nid_file_name = 'nid_'.time().'.pdf';
            $nid_file = $request->file('nid_file');
            $nid_file->move('assets/images/doctor/nid_file',$nid_file_name);

            $doctor->nid_file = $nid_file_name;
        }
        if($request->hasFile('cv_file')){
            @unlink('assets/images/doctor/cv_file/'.$request->cv_file);
            $cv_file_name = 'cv_'.time().'.pdf';
            $cv_file = $request->file('cv_file');
            $cv_file->move('assets/images/doctor/cv_file',$cv_file_name);

            $doctor->cv_file = $cv_file_name;
        }
        if($request->hasFile('picture'))
        {
            @unlink('assets/images/doctor/picture/'.$request->picture);
            $picture_name = 'pic_'.time().'.jpg';
            $picture_path = 'assets/images/doctor/picture/'.$picture_name;
            Image::make($request->file('picture'))->resize(200, 200)->save($picture_path);
            $doctor->picture = $picture_name;
        }



        $doctor->status = $request->status;
        $doctor->save();
        return redirect()->to(route('admin.doctor'))->with('success','Data has been updated');

    }
    public function view($id){
        $doctor =$this->doctor->findOrFail($id);
        return view('admin.doctor.view',compact('doctor'));
    }
}

更新:感谢所有经历过这一重要帖子并回答的人。

5 个答案:

答案 0 :(得分:1)

您收到此错误,因为在迁移中列 first_name 不可为空。这意味着您必须为此列提供一个值。

在您的表单中将这些字段设为必填是一种很好的做法,因此用户必须填写这些字段。另外,您可以利用服务器端validation来验证发布的请求。

答案 1 :(得分:1)

$doctor->first_name = $request->input('fname');

应使用与输入名称相同的形式访问请求属性,因此为“ first_name”

$doctor->first_name = $request->input('first_name');

这也适用于:

$doctor->last_name = $request->input('lname');

应该是:

$doctor->last_name = $request->input('last_name');

答案 2 :(得分:0)

异常Illuminate \ Database \ QueryException (23000) SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'first_name' cannot be null 本身表示错误,列first_name不能为空。您必须将列first_name设置为可空,或者必须插入数据。 检查表单,是否正确提交数据,然后从插入数据的位置检查控制器。

在保存数据之前在控制器中使用Validator。这是示例,

$validator = Validator::make($request->input() , [
    'first_name' => 'required',
    'last_name' => 'required',
     ........
])

然后您可以按照验证者的身份检查您的请求

if($validation->fails()){
    //Validation failed
}else{
    //Success, process the request data
}

答案 3 :(得分:0)

如果您在表单中看到input last_name的名字是last_name

在您的controller中,您正在寻找错误的$request->input('lname');

要么更改此

<input type="text" class="form-control" id="last_name" name="last_name" placeholder="Last Name">

<input type="text" class="form-control" id="last_name" name="lname" placeholder="Last Name">

或者在您的控制器中,您可以代替$request->input('lname');来执行此操作,而只能使用$request->input('last_name');firstname也会如此

谢谢

答案 4 :(得分:0)

$ request-> input('first_name');或$ request-> first_name;