我对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'));
}
}
更新:让它工作,感谢每个人谁通过这个大职位,并回答。
最佳答案
$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');
关于mysql - Illuminate\Database\QueryException(23000)SQLSTATE [23000]:违反完整性约束:1048列“first_name”不能为空,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57266012/