2017 © Pedro Peláez
 

library eloquent-relations

Eloquent Relationship that defines symmetric relations

image

boukeversteegh/eloquent-relations

Eloquent Relationship that defines symmetric relations

  • Thursday, November 19, 2015
  • by boukeversteegh
  • Repository
  • 1 Watchers
  • 0 Stars
  • 1,467 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 1 % Grown

The README.md

EloquentRelations

Adds a new type of relationship to Eloquent (the ORM that Laravel uses) that matches either of two foreign keys., (*1)

composer require boukeversteegh/eloquent-relations dev-master

HasManySymmetric

Eloquent Relationship that defines symmetric relations. Implemented as a HasMany relation with two candidate foreign keys., (*2)

The relation is equivalent to the following join:, (*3)

SELECT * FROM parent
JOIN related ON (parent.id = related.foreign_key_1 OR parent.id = related.foreign_key_2)

Use cases:, (*4)

  • Friendships (id, inviting_user_id, receiving_user_id): $user->friendships
  • Messages (id, receiver_user_id, sending_user_id): $user->messages
  • Games (id, home_team_id, away_team_id): $team->games

Usage

Use the provided trait to support symmetric relations, and define a relationship., (*5)

class User extends \Illuminate\Database\Eloquent\Model
{
    use \EloquentRelations\HasManySymmetricTrait;

    public function friendships()
    {
        return $this->hasManySymmetric(Friendship::class, ['inviting_user_id', 'receiving_user_id']);
    }
}
# Lazy load the relationship
$user = User::find(1);
$user->friendships;

# Eager loading
$user = User::with('friendships')->find(1);
$user->friendships;

The Versions

19/11 2015

dev-master

9999999-dev

Eloquent Relationship that defines symmetric relations

  Sources   Download

MIT