1対1
1対多
多対多
Has Many Through
1対1(ポリモーフィック)
1対多(ポリモーフィック)
多対多(ポリモーフィック)
これらのうち 1対1 リレーションを操作してみます。
呼び出す側に記述します。
/**
* ● 1対1リレーション
* 対象モデル「App\Doctor」
*
*/
public function doctor()
{
return $this->hasOne('App\Doctor','id','doctor_id')->withDefault();
}
/**
* 1対1リレーション(hasOne) : ->created_user で作成したユーザーの情報を取得できるようにする
*
* @return \Illuminate\Database\Eloquent\Relations\hasOne
*/
public function created_user()
{
return $this->hasOne('App\User', 'id', 'created_user_id')->withDefault();
}
呼び出される側に記述します。
/**
* ● 1対1リレーション(belongsTo) : ->mt_branch で営業支店の情報を取得できるようにする
* 対象モデル「App\MtBranch」
*
*/
public function mt_branch()
{
return $this->belongsTo('App\MtBranch','branch_id')->withDefault();
}