2在Laravel Eloquent中,ManyToMany关系+ 1个参数

时间:2016-01-15 18:31:41

标签: laravel eloquent

我有2张表A和B.

我与一个名为A_B

的数据透视表建立了多对多的关系

现在我需要在A_B和用户(U)之间建立其他ManyToMany关系

所以我有另一张桌子A_B_U

现在,我需要和不能做的是写一个从A到A_B_U的关系。

我将U设置为谷值,因此A_B - > A_B_U现在是一个belongsTo Relationship,我应该可以轻松访问它,但我不能写它,我可以写第一个关系,但是,我不能写第二个关系。

我不知道如何在ManyToMany Rel。中传递一个参数。

事实上,它应该是属于一个,而不是很多,所以我有点迷失......

任何想法怎么做?

1 个答案:

答案 0 :(得分:3)

它看起来像什么

            category_tournament_user
                   /        \
    category_tournament    users
        /       \
tournaments   categories

迁移

Schema::create('tournaments', function(Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->timestamps();
});

Schema::create('categories', function(Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->timestamps();
});

Schema::create('category_tournament', function(Blueprint $table) {
    $table->increments('id');
    $table->integer('category_id');
    $table->integer('tournament_id');
    $table->timestamps();
});

Schema::create('category_tournament_user', function(Blueprint $table) {
    $table->integer('category_tournament_id');
    $table->integer('user_id');
});

模型

class Tournament extends Model
{
    public function categories()
    {
        return $this->belongsToMany(Category::class);
    }

    public function categoryTournaments()
    {
        return $this->hasMany(CategoryTournament::class);
    }
}

class Category extends Model
{
    public function tournaments()
    {
        return $this->belongsToMany(Tournament::class);
    }

    public function categoryTournament()
    {
        return $this->hasMany(CategoryTournament::class);
    }
}

class CategoryTournament extends Model
{
    protected $table = 'category_tournament';

    public function category()
    {
        return $this->belongsTo(Category::class);
    }

    public function tournament()
    {
        return $this->belongsTo(Tournament::class);
    }

    public function users()
    {
        return $this->belongsToMany(User::class, 'category_tournament_user');
    }
}

class User extends Authenticatable
{
    public function categoryTournaments()
    {
        return $this->belongsToMany(CategoryTournament::class, 'category_tournament_user');
    }
}

获取锦标赛的所有用户

$tournament = App\Tournament::with('categoryTournaments.users')->find(1);

foreach($tournament->categoryTournaments as $categoryTournament) {

    // To get the category of the tournament, this will return an App\Category
    $category = $categoryTournament->category;

    // If you have a name column on your categories table...
    $categoryName = $category->name;


    // To get the tournament.
    $tournament = $categoryTournament->tournament;

    // To get the name of the tourament (if there is a name column)
    $tournamentName = $tournament->name;
    $tournament->created_at; // etc...


    // To get all the people participating in this tournament category
    foreach($categoryTournament->users as $user) {
        echo $user->email;
    }
}

如果您需要弄清楚如何遍历关系(获取用户参与的所有锦标赛和类别等等),请随时提出。

从用户

开始
// This will list all tournament and category names a user belongs to.
$user = App\User::with('categoryTournaments.tournament', 'categoryTournaments.category')->find($userId);
foreach($user->categoryTournaments as $categoryTournament) {
    echo 'Category Name: '.$categoryTournament->category->name;
    echo 'Tournament Name: '.$categoryTournament->tournament->name;
    echo '<br>';
}

修改

  • users和category_tournament构成belongsToMany关系。
  • 类别和锦标赛构成了belongsToMany关系。
  • category_tournament还拥有属于锦标赛和类别的所有权

如果删除表tournamentscategories并将所有列放到category_tournament,我们可以进一步简化此操作。这样,您最终只能管理一个belongsToMany关系。