dongwoqin7034
2017-11-27 05:22
浏览 65
已采纳

laravel在尝试进行更新时返回null

I am trying to do an update with my data but when I try to pass $object to another view it is returning me null value. I did a dd($id) and it return me the id that I wanted but if I do a dd($object) return me null. I have check similar question to this such as Laravel Eloquent::Find() returning NULL with an existing ID but still doesn't work. Everyone say that the problem comes from the database, but I don't understand which part. Can anyone help me? Thanks a lot

VerificationController:

 public function editVerify($id){
    $object = Verification::find($id);
    dd($object);
    return view('editVerify', compact('object'));
}

Verification Model:

     public function personal_infos() {
        return $this->belongsTo("App\PersonalInfo", "id", 'user_id');
}

PersonalInfo model:

                public function verifications() {
return $this->hasOne('App\Verification','user_id');

}

This is what it look like in the database(some value are NULL while some contain data) enter image description here enter image description here Verification table:

  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();
        });
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douxunchen3498 2017-11-27 06:05
    已采纳

    When we use Eloquent find(), find method works with primary key. As you are using find() method to fetch data, it expects id as its parameter but you are looking to fetch records with user_id. So here is my solution-

    $v=Verification::where('user_id','=',$id)->first();
    

    or make user_id column your primary key.

    已采纳该答案
    打赏 评论
  • duanpu8830 2017-11-27 06:09

    Currently, you are working with find() of Eloquent, and it find record regarding to 'id', in your DB it not exists and it exists in your user_id column!

    So, change query to

    $v=Verification::where('user_id','=',$id)->first();
    

    And this gives you result OR change primary key to user_id

    Hope this helps you!

    打赏 评论

相关推荐 更多相似问题