Association in Laravel
Laravel supports associations. Here associations are basically relationship between tables. There are six types of associations in Laravel:
Relationship |
Type of Association |
Example |
|
|
|
One to One |
hasOne |
User has one profile. |
One to Many |
hasMany |
Blog has many posts. |
Many to Many |
belongsToMany |
Many users may have many roles. |
Has Many Through |
hasManyThrough |
Country may have many posts through an intermediate user. |
Polymorphic Relations |
morphTo |
Picture belongs to an employee or product. |
Many to Many Polymorphic Relations |
morphToMany |
Post and video can share polymorphic relation to a tag. |
One to One
one-to-one relation is a basic relation. For example: A supplier has only one account. Here account method should return the results of the hasOne method:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Supplier extends Model
{
/**
* Get the account record associated with the supplier.
*/
public function account()
{
return $this->hasOne('App\Account');
}
}
One to Many
one-to-many association indicates has many relation. This association indicates that each instance of model has zero or more instance of another model. Example: A Blog model can have many posts and posts model may have many comments:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Blog extends Model
{
/**
* Get the posts for the blog.
*/
public function posts()
{
return $this->hasMany('App\Post');
}
}
Many to Many
Many users can have many roles. Example: Many users can have role of Admin, Subadmin or Superadmin. Many-to-many association is defined by method that calls the belongsToMany method:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* The roles that belong to the user.
*/
public function roles()
{
return $this->belongsToMany('App\Role');
}
}
Has Many Through
Has-many-through association is used to set up many to many connections with another model. Country model have many Post models through an intermediate User model. Example: You can gather posts blog from a given country.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Country extends Model
{
/**
* Get all of the posts for the country.
*/
public function posts()
{
return $this->hasManyThrough('App\Post', 'App\User');
}
}
Polymorphic Relations
Polymorphic-relations are more advanced association provided by laravel. On a single association, a model can belong to more than one other model with polymorphic association. For example: Picture model can belong to employee model or a product model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Photo extends Model
{
/**
* Get all of the owning imageable models.
*/
public function imageable()
{
return $this->morphTo();
}
}
class Employee extends Model
{
/**
* Get all of the employees photos.
*/
public function photos()
{
return $this->morphMany('App\Photo', 'imageable');
}
}
class Product extends Model
{
/**
* Get all of the product's photos.
*/
public function photos()
{
return $this->morphMany('App\Photo', 'imageable');
}
}
Many to Many Polymorphic Relations
Blog Post and Video model can share polymorphic relation to Tag model. List of unique tags shared across blog posts and videos is allowed by many-to-many polymorphic relations:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
/**
* Get all of the tags for the post.
*/
public function tags()
{
return $this->morphToMany('App\Tag', 'taggable');
}
}
Thanks for reading.
0 Comment(s)