我正在尝试使用我的数据进行更新,但是当我尝试将$ object传递给另一个视图时,它返回null值。我做了一个dd($ id)并且它返回了我想要的id但是如果我做了一个dd($ object)则返回null。我已经检查了类似的问题,如Laravel Eloquent::Find() returning NULL with an existing ID,但仍然无法正常工作。每个人都说问题来自数据库,但我不明白哪个部分。谁能帮我?非常感谢
验证控制器:
public function editVerify($id){
$object = Verification::find($id);
dd($object);
return view('editVerify', compact('object'));
}
验证模型:
public function personal_infos() {
return $this->belongsTo("App\PersonalInfo", "id", 'user_id');
}
PersonalInfo模型:
public function verifications() {
return $this->hasOne('App\Verification','user_id');
}
这是它在数据库中的样子(某些值为NULL而某些值包含数据) 验证表:
Schema::create('verifications', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('Nationality');
$table->string('identity_type');
$table->string('residential_status');
$table->string('NRIC_check');
$table->string('nric_number1');
$table->string('blacklist');
$table->string('whiteCard');
$table->string('educational_cert');
$table->string('WSQ_cert');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('personal_infos');
$table->timestamps();
});
当我们使用Eloquent find()
时,find方法适用于primary key
。当您使用find()
方法获取数据时,它期望id
作为其参数,但您希望使用user_id
获取记录。所以这是我的解决方案 -
$v=Verification::where('user_id','=',$id)->first();
或者使user_id
列你的primary key
。
目前,您正在使用Eloquent的find()
,它找到了有关'id'
的记录,在您的数据库中它不存在,它存在于您的user_id
列中!
所以,将查询更改为
$v=Verification::where('user_id','=',$id)->first();
这会给你结果或更改user_id
的主键
希望这对你有所帮助!