LoginSignup
4
1

More than 3 years have passed since last update.

LaraveのhasOneとbelongsToの違い

Posted at

参照先 or 参照される側 or 外部キーがある側は、belongsTo
参照元 or 参照する側 or 外部キーがない側は、hasOne

UsersMedel.php
[id,name] //だとする
    public function profile()
    {
        return $this->hasOne(Profiles::class, 'user_id', 'id');
    }
ProfilesMedel.php
[id,memo,address,user_id] //だとする
    public function profile()
    {
        return $this->belongsTo(Users::class, 'user_id', 'id');
    }
4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1