我正在使用PHP,Laravel 5和MySQL。我试图基于经过身份验证的用户获取“患者”,但是主键值错误。
在PatientController中,我运行以下命令:
$ Patient = Patient :: where('userId','=',
$ request-> user()-> id)-> get()-> first();
刀片中PatientId的值与存储的值不同。
例如,我得到3127
而原始值为3127fc14-368e-4b06-90f2-a524986e627e
我不会在其余的列上遇到同样的问题。
感谢您的时间和精力。
User.php
class User extends Authenticatable
{
protected $fillable = [
'id','name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
public function patient()
{
return $this->belongsTo('App\Patient', 'patientId','id');
}
}
UserController.php
class UserController extends Controller
{
/**
* @param Request $request
* @return \Illuminate\Http\RedirectResponse
*/
public function postSignUp(Request $request)
{
$this->validate($request,[
'email' => 'required|email|unique:users',
'name' => 'required|max:100',
'password' => 'required|min:6'
]);
$user = new User();
$user->email = $request['email'];
$user->name = $request['name'];
$user->password = bcrypt($request['password']);
Auth::login($user);
return redirect()->route('dashboard');
}
/**
* @param Request $request
* @return \Illuminate\Http\RedirectResponse
*/
public function postSignIn(Request $request)
{
$this->validate($request,[
'email' => 'required',
'password' => 'required'
]);
if (Auth::attempt([ 'email' => $request['email'], 'password' => $request['password']] )){
return redirect()->route('dashboard');
}
return redirect()->back();
}
}
Patient.php
class Patient extends Model
{
protected $primaryKey='patientId';
public $incrementing = 'false';
public $timestamps = true;
protected $fillable = [
'patientId','contactId','guardianId','userId','patientNationalId','patientFirstName','patientSurName','patientDob','patientInsuranceNumber','patientGender'
];
public function user()
{
return $this->hasOne('App\User','id','userId');
}
}
PatientController.php
class PatientController extends Controller
{
public function getDashboard(Request $request)
{
$patient = Patient::where('userId', '=', $request->user()->id)->get()->first();
return view('dashboard', [
'patient' => $patient
]);
}
}
移居
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
});
Schema::create('patients', function (Blueprint $table) {
$table->string('patientId')->primary();
$table->string('contactId')->index()->nullable();
$table->string('guardianId')->index()->nullable();
$table->integer('userId')->unsigned()->nullable();
$table->string('patientNationalId')->nullable();
$table->string('patientFirstName')->nullable();
$table->string('patientSurName')->nullable();
$table->string('patientDob')->nullable();
$table->string('patientGender')->nullable();
$table->string('patientInsuranceNumber')->nullable();
$table->timestamps();
# Foreign Keys
$table->foreign('userId')->references('id')->on('users');
$table->foreign('guardianId')->references('guardianId')->on('guardians');
$table->foreign('contactId')->references('contactId')->on('contacts');
});
最佳答案
您具有userId作为外键,它引用用户中的id(它是整数)。
当您说原始值是3127fc14-368e-4b06-90f2-a524986e627e
时,如何创建该id
?
您的架构将列的类型描述为整数,因此3127fc14-368e-4b06-90f2-a524986e627e只会导致integer
值为3127