One-to-One Relationship
One to One model relationship is very simple and basic. you have to make sure that one of the table has a key that references the id of the other table. we will learn how we can create migration with foreign key schema, retrieve records, insert new records, update records etc.

class User extends Authenticatable implements MustVerifyEmail
{
public function info()
{
return $this->hasOne('App\Phone','user_id','id');
}
}
One-to-Many Relationship
One to Many relationship will use when one table associated with multiple tables. For example, a post may have multiple comments.

use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
public function comment()
{
return $this->hasMany('App\Comment','post_id','id');
}
}
Many-to-One Relationship (Inverse of One-to-Many Relationship)
class Comment extends Model
{
public function post()
{
return $this->belongsTo('App\Post','post_id','id');
}
}
Many-to-Many Relationship
Many to many relationship is a little bit complicated than one to one and one to many relationships. An example of such a relationship is a user with may have multiple roles, where the role are also connected with multiple users.

use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
public function roles()
{
return $this->belongsToMany('App\Role', 'role_user','user_id','role_id');
}
}
has One Through – Relationship

The “has-one-through” relationship links models through a single intermediate relation. For example, if each supplier has one user, and each user is associated with one user history record, then the supplier model may access the user’s history through the user. Let’s look at the database tables necessary to define this relationship:
class Supplier extends Model
{
public function userHistory()
{
return $this->hasOneThrough(
'App\History',
'App\User',
'supplier_id', // Foreign key on users table...
'user_id', // Foreign key on history table...
'id', // Local key on suppliers table...
'id' // Local key on users table...
);
}
}
has Many Through – Relationship
Has Many Through relationship is a bit complicated to understand a provide shortcut way to access data of another mode relation. For example, a country is connected with users and users with posts, then we can access all posts connected with a specific country.
The “has-many-through” relationship provides a convenient shortcut for accessing distant relations via an intermediate relation. For example, a Country
model might have many Post
models through an intermediate User
model. In this example, you could easily gather all blog posts for a given country. Let’s look at the tables required to define this relationship:

class Country extends Model
{
public function posts()
{
return $this->hasManyThrough(
'App\Post',
'App\User',
'country_id', // Foreign key on users table...
'user_id', // Foreign key on posts table...
'id', // Local key on countries table...
'id' // Local key on users table...
);
}
}
OTHER EXAMPLE:
User Model:
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable implements MustVerifyEmail
{
public function info(){
return $this->hasOne('App\Info','user_id','id');
}
public function article(){
return $this->hasMany('App\Article','user_id','id');
}
public function category(){
return $this->belongsToMany('App\Category','category_user','user_id','category_id');
}
}
Article Model:
class Article extends Model
{
public function user()
{
return $this->belongsTo('App\User','user_id','id');
}
public function category()
{
return $this->belongsTo('App\Category','category_id','id');
}
}
Category Model
class Category extends Model
{
public function user(){
return $this->belongsToMany('App\User','category_user','category_id','user_id');
}
}
CategoryUser Model
class CategoryUser extends Model
{
protected $table = ‘category_user’;
}