In this lesson, I will show you how to use Eloquent Relationships in laravel applications. I will show you how to build one-to-one, one-to-many, many-to-many, polymorphic relations across the laravel eloquent model class.
$this->hasOne('App\Model');
$this->hasOne('App\Model', 'foreign_key', 'local_key');
One To One (Inverse):
$this->belongsTo('App\Model');
$this->belongsTo('App\Model', 'foreign_key', 'other_key');
$this->hasMany('App\Model');
$this->hasMany('App\Model', 'foreign_key', 'local_key');
One To Many (Inverse)
$this->belongsTo('App\Model');
$this->belongsTo('App\Model', 'foreign_key', 'other_key');
$this->belongsToMany('App\Model', 'pivot_table', 'foreign_id', 'foreign_id');
Example:
$this->belongsToMany('App\Role', 'role_user', 'user_id', 'role_id');
Attaching / Detaching:
Attaching:
$user->roles()->attach($roleId);
$user->roles()->attach($roleId, ['expires' => $expires]);
$user->roles()->attach([
1 => ['expires' => $expires],
2 => ['expires' => $expires],
]);
Detaching:
// Detach a single role from the user...
$user->roles()->detach($roleId);
// Detach all roles from the user...
$user->roles()->detach();
$user->roles()->detach([1, 2, 3]);
Syncing Associations:
$user->roles()->sync([1, 2, 3]);
$this->hasOneThrough(
'App\Owner',
'App\Car',
'mechanic_id', // Foreign key on cars table...
'car_id', // Foreign key on owners table...
'id', // Local key on mechanics table...
'id' // Local key on cars table...
);
mechanics
id - integer
name - string
cars
id - integer
model - string
mechanic_id - integer
owners
id - integer
name - string
car_id - integer
$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...
);
countries
id - integer
name - string
users
id - integer
country_id - integer
name - string
posts
id - integer
user_id - integer
title - string
One To One (Polymorphic):
posts
id - integer
name - string
users
id - integer
name - string
images
id - integer
url - string
imageable_id - integer
imageable_type - string
Models:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Image extends Model
{
/**
* Get the owning imageable model.
*/
public function imageable()
{
return $this->morphTo();
}
}
class Post extends Model
{
/**
* Get the post's image.
*/
public function image()
{
return $this->morphOne('App\Image', 'imageable');
}
}
class User extends Model
{
/**
* Get the user's image.
*/
public function image()
{
return $this->morphOne('App\Image', 'imageable');
}
}
One To Many (Polymorphic):
posts
id - integer
title - string
body - text
videos
id - integer
title - string
url - string
comments
id - integer
body - text
commentable_id - integer
commentable_type - string
Models:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
/**
* Get the owning commentable model.
*/
public function commentable()
{
return $this->morphTo();
}
}
class Post extends Model
{
/**
* Get all of the post's comments.
*/
public function comments()
{
return $this->morphMany('App\Comment', 'commentable');
}
}
class Video extends Model
{
/**
* Get all of the video's comments.
*/
public function comments()
{
return $this->morphMany('App\Comment', 'commentable');
}
}