dev-master
9999999-devMany-to-many relations between 3 models
MIT
The Requires
- php >=5.4.0
- illuminate/support 4.2.*
Wallogit.com
2017 © Pedro Peláez
Many-to-many relations between 3 models
This package is not maintained anymore, let me know if you wish to work on it, (*1)
A way to link 3 many-to-many relations together in Laravel 4's Eloquent., (*2)
composer.json and config/app.php
->tripleBelongsToMany()
->third() method// 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
Models/User.php (should already exist), (*3)
<?php
namespace Models;
class User extends \Eloquent {
}
Models/Tag.php, (*4)
<?php
namespace Models
class Tag extends \Eloquent {
}
Models/Track.php, (*5)
<?php
namespace Model;
class Track extends \Eloquent {
}
database/migrations/1_0_0_0_create_triple_pivot_tables.php: Create the tables we're going to be joining together (users may already have a migration)., (*6)
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class CreateTriplePivotTables extends Migration {
public function down() {
Schema::drop( 'users' );
Schema::drop( 'tags' );
Schema::drop( 'tracks' );
Schema::drop( 'users_tags_tracks' );
}
public function up() {
Schema::create( 'users', function ( Blueprint $table ) {
$table->increments('id');
$table->string('email');
} );
Schema::create( 'tags', function ( Blueprint $table ) {
$table->increments('id');
$table->string('name');
} );
Schema::create( 'tracks', function ( Blueprint $table ) {
$table->increments('id');
$table->string('name');
} );
Schema::create( 'users_tags_tracks', function ( Blueprint $table ) {
$table->integer( 'user_id' )->unsigned()->nullable();
$table->integer( 'tag_id' )->unsigned()->nullable();
$table->integer( 'track_id' )->unsigned()->nullable();
} );
}
}
composer.json: Add in the package definition., (*7)
"require": {
"laravel/framework": "4.2.*",
...
"jarektkaczyk/eloquent-triple-pivot": "dev-master"
},
Run composer update -o in the Terminal., (*8)
config/app.php: Add the service provider to the providers array., (*9)
'providers' => array(
'Illuminate\Foundation\Providers\ArtisanServiceProvider',
...
'Jarektkaczyk\TriplePivot\TriplePivotServiceProvider',
),
Models/User.php: Two use statements - one to pull in the namespaced Trait, one to use it in the Model., (*10)
<?php
namespace Models;
use Jarektkaczyk\TriplePivot\TriplePivotTrait;
class User extends \Eloquent {
use TriplePivotTrait;
}
Models/Tag.php: As above, (*11)
<?php
namespace Models;
use Jarektkaczyk\TriplePivot\TriplePivotTrait;
class Tag extends \Eloquent {
use TriplePivotTrait;
}
Models/Track.php: As above, (*12)
<?php
namespace Models;
use Jarektkaczyk\TriplePivot\TriplePivotTrait;
class Track extends \Eloquent {
use TriplePivotTrait;
}
tripleBelongsToMany relationModels/User.php, (*13)
<?php
namespace Models;
use Jarektkaczyk\TriplePivot\TriplePivotTrait;
class User extends \Eloquent {
use TriplePivotTrait;
/**
* @return \Jarektkaczyk\TriplePivot\TripleBelongsToMany
*/
public function tags() {
return $this->tripleBelongsToMany( 'Models\Tag', 'Models\Track', 'users_tags_tracks' );
}
}
->third on Models/Tag
Models/Tag.php: Create a new method getTrackAttribute() which forwards on to the getThirdAttribute() method so we can call $tag->track->name instead of $tag->third->name., (*14)
<?php
namespace Models;
use Jarektkaczyk\TriplePivot\TriplePivotTrait;
class Tag extends \Eloquent {
use TriplePivotTrait;
/**
* @return \Illuminate\Database\Eloquent\Model
*/
public function getTrackAttribute() {
return $this->getThirdAttribute();
}
}
Many-to-many relations between 3 models
MIT