0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Laravelのリレーション

Laravelのリレーションはモデルに書くことで定義するらしい
・1対1
・1対多
・多対多
パターンはこの3種くらいか。

1対1

public function ○○○○○(){
return $this->hasOne('App\Models\○○○○○');
}
public function □□□□□(){
return $this->belongsTo('App\Models\□□□□□');
}

1対多

public function ○○○○○(){
return $this->hasMany('App\Models\○○○○○');
}
public function □□□□□(){
return $this->belongsTo('App\Models\□□□□□');
}

多対多

Public function roles()
{
    return $this->belognsToMany(Role::class);
}
return $this->belongsToMany(Role::class, 'role_user', 'user_id', 'role_id')

↓このあたりを使い分けるっぽい
hasOne
belongsTo
hasMany
belognsToMany

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?