PHPackages                             jarektkaczyk/eloquent-triple-pivot - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. jarektkaczyk/eloquent-triple-pivot

ActiveLibrary

jarektkaczyk/eloquent-triple-pivot
==================================

Many-to-many relations between 3 models

431.1k15[2 issues](https://github.com/jarektkaczyk/Eloquent-triple-pivot/issues)PHP

Since Feb 1Pushed 9y ago5 watchersCompare

[ Source](https://github.com/jarektkaczyk/Eloquent-triple-pivot)[ Packagist](https://packagist.org/packages/jarektkaczyk/eloquent-triple-pivot)[ RSS](/packages/jarektkaczyk-eloquent-triple-pivot/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Triple Pivot
============

[](#triple-pivot)

**This package is not maintained anymore, let me know if you wish to work on it**

A way to link 3 many-to-many relations together in Laravel 4's Eloquent.

---

Contents
--------

[](#contents)

### Usage

[](#usage)

### Setup

[](#setup)

1. Create 3 models: User, Tag, Track
2. Set up your tables: users, tags, tracks, pivot table (defaults to tag\_track\_user, in the example below we use custom name users\_tags\_tracks)
3. Require in `composer.json` and `config/app.php`
4. Add the trait in all 3 models
5. Define the relation method as `->tripleBelongsToMany()`
6. (Optional) Create a nice-name relation for the `->third()` method

---

Usage
-----

[](#usage-1)

```
// Get the first User, and autoload their tags
$user = User::with( 'tags' )->first();

// Like an ordinary belongsToMany
$user->tags; // Collection of tags
$user->tags->first(); // Tag model

// Get the track associated with a given tag for the user
$user->tags->first()->third; // Track model
$user->tags->first()->track; // Track model (only if you did step 6)

// Attach a tag/track to a user
$user->tags()->attach( [ $tagId, $trackId ] ); // Pass an array of 2 IDs
$user->tags()->attach( [ Tag::find( $tagId ), Track::find( $trackId ) ] ); // Pass an array of 2 models

```

---

Setup
-----

[](#setup-1)

### 1. Create 3 models

[](#1-create-3-models)

**Models/User.php** (should already exist)

```
