Laravel Relationships CheatSheet

I have a lot of confusing when I starting to deal with laravel relationships, so small CheatSheet to myself and you.

hasOne + belongsTo

class Client extends Model
{
    public function phone()
    {
        return $this->hasOne('App\Phone');
    }
}s

Searching client_id into phones table and returns one row from table.

class Phone extends Model
{
    public function client()
    {
        return $this->belongsTo('App\Client');
    }
}

Also searching client_id into phones table and returns one row from table.

hasMany + belongsTo

class Client extends Model
{
    public function emails()
    {
        return $this->hasMany('App\Email');
    }
}

Searching client_id into emails table and returns all matched rows from table.

class Email extends Model
{
    public function client()
    {
        return $this->belongsTo('App\Client');
    }
}

Searching client_id into emails table and returns one row from table.

belongsToMany + belongsToMany

class Discount extends Model
{
    public function clients()
    {
        return $this->belongsToMany('App\Client');
    }
}

Searching client_id and discount_id into clients_discounts table and returns all matched rows from table.

class Client extends Model
{
    public function discounts()
    {
        return $this->belongsToMany('App\Discount');
    }
}

Searching client_id and discount_id into clients_discounts table and returns all matched rows from table.

hasManyThrough

class Poll extends Model
{
    public function answers()
    {
        return $this->hasManyThrough('App\Question', 'App\Answer');
    }
}

Searching question_id into answers table and then poll_id into questions table. Returns all rows from answers table which belongs to this poll.

morphTo + morphMany

class Note extends Model
{
    public function notable()
    {
        return $this->morphTo();
    }
}

Mark model as polymorphic. Returns all models which have notes.

class Invoice extends Model
{
    public function notes()
    {
        return $this->morphMany('App\Note', 'notable');
    }
}

Searches for 'App\Invoice' string into notable_type column and invoice id into notable_id and returns all rows (notes) which belongs to that invoice.

class Conversation extends Model
{
    public function notes()
    {
        return $this->morphMany('App\Note', 'notable');
    }
}

Searches for 'App\Conversation' string into notable_type column and conversation id into notable_id and returns all rows (notes) which belongs to that conversation.

morphToMany + morphedByMany

class Item extends Model
{
    public function categories()
    {
        return $this->morphToMany('App\Category', 'categorizable');
    }
}

Searches for 'App\Item' string into categorizable_type column and item id into categorizable_id column and returns all rows (categories) which belongs to that item.

class Post extends Model
{
    public function categories()
    {
        return $this->morphToMany('App\Category', 'categorizable');
    }
}

Searches for 'App\Post' string into categorizable_type column and post id into categorizable_id column and returns all rows (categories) which belongs to that post.

class Image extends Model
{
    public function categories()
    {
        return $this->morphToMany('App\Category', 'categorizable');
    }
}

Searches for 'App\Image' string into categorizable_type column and image id into categorizable_id column and returns all rows (categories) which belongs to that image.

class Category extends Model
{
    public function items()
    {
        return $this->morphedByMany('App\Item', 'categorizable');
    }


    public function posts()
    {
        return $this->morphedByMany('App\Post', 'categorizable');
    }


    public function images()
    {
        return $this->morphedByMany('App\Image', 'categorizable');
    }
}

Searches for 'App\Item'/'App\Post'/'App\Image' string into categorizable_type column and item/post/image id into categorizable_id column and returns all rows (items/posts/images) which belongs to that category.

Official documentation at Official documentation of Laravel Relationships

Thats it. Hope it helps.

Share on FacebookShare on RedditShare on Google+Tweet about this on TwitterShare on LinkedIn
don't waste your time using copy/paste, just use share buttons above
Laravel Relationships CheatSheet
  • Justas Maziliauskas

    I just leave it here… to make sure comments are working

  • Haneef Ansari

    Awesome.. Explanation..