jarektkaczyk / eloquent-triple-pivot
3个模型之间的多对多关系
dev-master
2016-02-01 21:41 UTC
Requires
- php: >=5.4.0
- illuminate/support: 4.2.*
This package is auto-updated.
Last update: 2024-09-07 15:28:11 UTC
README
此包不再维护,如果您愿意参与维护,请告知
在 Laravel 4 的 Eloquent 中链接 3 个多对多关系的方法。
内容
用法
设置
- 创建 3 个模型:User,Tag,Track
- 设置您的表:users,tags,tracks,关联表(默认为 tag_track_user,以下示例中使用自定义名称 users_tags_tracks)
- 在
composer.json
和config/app.php
中引入 - 将特性添加到所有 3 个模型中
- 将关系方法定义为
->tripleBelongsToMany()
- (可选) 为
->third()
方法创建一个别名
用法
// 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
设置
1. 创建 3 个模型
Models/User.php (应该已经存在)
<?php
namespace Models;
class User extends \Eloquent {
}
Models/Tag.php
<?php
namespace Models
class Tag extends \Eloquent {
}
Models/Track.php
<?php
namespace Model;
class Track extends \Eloquent {
}
2. 设置数据库表
database/migrations/1_0_0_0_create_triple_pivot_tables.php:创建将要连接的表(users
可能已经有了迁移)。
<?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:添加包定义。
"require": {
"laravel/framework": "4.2.*",
...
"jarektkaczyk/eloquent-triple-pivot": "dev-master"
},
在终端中运行 composer update -o
。
config/app.php:将服务提供者添加到 providers
数组。
'providers' => array(
'Illuminate\Foundation\Providers\ArtisanServiceProvider',
...
'Jarektkaczyk\TriplePivot\TriplePivotServiceProvider',
),
4. 将特性添加到所有模型中
Models/User.php:两个 use
语句 - 一个用于引入命名空间中的特性,另一个用于在模型中使用它。
<?php
namespace Models;
use Jarektkaczyk\TriplePivot\TriplePivotTrait;
class User extends \Eloquent {
use TriplePivotTrait;
}
Models/Tag.php:与上面相同
<?php
namespace Models;
use Jarektkaczyk\TriplePivot\TriplePivotTrait;
class Tag extends \Eloquent {
use TriplePivotTrait;
}
Models/Track.php:与上面相同
<?php
namespace Models;
use Jarektkaczyk\TriplePivot\TriplePivotTrait;
class Track extends \Eloquent {
use TriplePivotTrait;
}
5. 定义 tripleBelongsToMany
关系
Models/User.php
<?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' );
}
}
6. (可选) 在 Models/Tag
中定义一个比 ->third
更好的方法
Models/Tag.php:创建一个新的方法 getTrackAttribute()
,它将转发到 getThirdAttribute()
方法,因此我们可以调用 $tag->track->name
而不是 $tag->third->name
。
<?php
namespace Models;
use Jarektkaczyk\TriplePivot\TriplePivotTrait;
class Tag extends \Eloquent {
use TriplePivotTrait;
/**
* @return \Illuminate\Database\Eloquent\Model
*/
public function getTrackAttribute() {
return $this->getThirdAttribute();
}
}