Laravel Relationships: Defining Relationships

Defining Relationships

$user->posts()->where('active', 1)->get();

Defined as methods on your Eloquent model classes.

One To One

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    /**
     * Get the phone record associated with the user.
     */
    public function phone()
    {
        return $this->hasOne('App\Phone');
    }
}
$phone = User::find(1)->phone;
Return $this->hasOne('App\Phone', 'foreign_key');
Return $this->hasOne('App\Phone', 'foreign_key', 'local_key');

One To Many

Is used to define relationships where a single model owns any amount of other models.

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    /**
     * Get the comments for the blog post.
     */
    public function comments()
    {
        return $this->hasMany('App\Comment');
    }
}
Return $this->hasMany('App\Comment', 'foreign_key');

return $this->hasMany('App\Comment', 'foreign_key', 'local_key');
$comments = App\Post::find(1)->comments;

foreach ($comments as $comment) {
    //
}
$comment = App\Post::find(1)->comments()->where('title', 'foo')->first();

One To Many (Inverse)

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Comment extends Model
{
    /**
     * Get the post that owns the comment.
     */
    public function post()
    {
        return $this->belongsTo('App\Post');
    }
}
/**
 * Get the post that owns the comment.
 */
public function post()
{
    return $this->belongsTo('App\Post', 'foreign_key');
}
/**
 * Get the post that owns the comment.
 */
public function post()
{
    return $this->belongsTo('App\Post', 'foreign_key', 'other_key');
}
$comment = App\Comment::find(1);

echo $comment->post->title;

Many To Many

<?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');
    }
}
Return $this->belongsToMany('App\Role', 'role_user');
Return $this->belongsToMany('App\Role', 'role_user', 'user_id', 'role_id');
$user = App\User::find(1);

foreach ($user->roles as $role) {
    //
}
$roles = App\User::find(1)->roles()->orderBy('name')->get();

Has Many Through

Provides a convenient shortcut for accessing distant relations via an intermediate relation.

Countries
    id - integer
    name - string

users
    id - integer
    country_id - integer
    name - string

posts
    id - integer
    user_id - integer
    title - string
<?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');
    }
}
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...
        );
    }
}

Polymorphic Relations

Allow a model to belong to more than one other model on a single association.

Many To Many Polymorphic Relations

Retrieving The Relationship

$post = App\Post::find(1);

foreach ($post->tags as $tag) {
    //
}
$tag = App\Tag::find(1);

foreach ($tag->videos as $video) {
    //
}

Laravel Relationships: Defining Relationships — Structure map

Clickable & Draggable!

Laravel Relationships: Defining Relationships — Related pages: