【问题标题】:(Laravel) Polymorphic relation through pivot table(Laravel)通过数据透视表的多态关系
【发布时间】:2019-03-05 21:19:46
【问题描述】:

假设我有一个 Event 模型,它通过多态关系和数据透视表 (EventParticipant) 包含更多各种模型的参与者(Player、Coach、Admin),其中还包含一个布尔列 participate。我想通过 $event->participants 获取参与者,它通过多态关系检索球员、教练和管理员的集合。

我在训练中使用标准的非多态关系创建了类似的东西,如下所示:

class Training extends Model
{
    /**
    * Training has more players.
    */
    public function players() {
        return $this->belongsToMany('App\Player', 'training_player')
            ->using('App\TrainingPlayer')
            ->withPivot('participate');
    }
}

class TrainingPlayer extends Pivot
{
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'participate' => 'boolean'
    ];
}

如何在事件的情况下进行修改,参与者()可以是 Player、Coach 或 Admin 模型?(也许是 MorphPivot 类,但我无法想象如何修改。 )

(而不是player_id(在TrainingPlayer 类中)指的是Player 模型的id,有两列rolerollable_id(在EventParticipant 类中)指的是Player 的id 、Coach 或 Admin 模型)

class Event extends Model
{
    /**
    * Event has more participants (players, coaches, or admins).
    */
    public function participants() {
        //
    }
}

class EventParticipant extends MorphPivot
{
    //
}

任何帮助将不胜感激。 :) 谢谢

【问题讨论】:

  • 你的意思是laravel.com/docs/…
  • 是的,但特别是我的意思是“定义自定义中间表模型”标题下的段落,其中说明了使用 Illuminate\Database\Eloquent\Relations\MorphPivot 类但我不知道在我的情况下如何准确实现它,因为文档这里也很不足。
  • 不可能通过单一关系获得所有不同的模型(球员、教练、管理员)。每个模型需要一个 MorphedByMany 关系。
  • 哦,很高兴知道@JonasStaudenmeir。我试图在几个小时内找到一个解决方案,但我最终得到了 3 个 belongsToMany() 关系(球员、教练、管理员),其中包含 wherePivot('role', ...) 条件......以及方法参与者()合并了所有上述集合,但显然参与者()返回一个集合而不是查询构建器,所以我不能指定这样的查询:$event->participants()->with('user_account')->get(),所以需要一种解决方法......

标签: laravel polymorphism pivot relationship


【解决方案1】:

我一直在寻找类似的东西并想出了一个解决方案。根据 Jonas 的评论,您不能在 1 个相关集中拥有不同的模型,但您可以使用 1 个数据透视表为每个模型拥有 1 个。

您现在可以使用 \App\Team::find(1)->with(['managers', 'users'])->get(); 查询此内容

Schema::create('associations', function (Blueprint $table) {
    $table->id();
    $table->string('association_type');
    $table->integer('association_id');
    $table->integer('team_id');
    $table->integer('meta')->nullable();
    $table->timestamps();
});

Schema::create('managers', function (Blueprint $table) {
    $table->id();
    $table->timestamps();
});

Schema::create('users', function (Blueprint $table) {
    $table->id();
    $table->timestamps();
});

Schema::create('teams', function (Blueprint $table) {
    $table->id();
    $table->timestamps();
});

class Manager extends Model
{
    public function teams()
    {
        return $this->belongsToMany('\App\Team', 'associations')->using('App\Association');
    }
}

class Team extends Model
{
    public function managers()
    {
        return $this->morphedByMany('App\Manager', 'association')->using('App\Association');
    }

    public function users()
    {
        return $this->morphedByMany('App\User', 'association')->using('App\Association');
    }
}

class User extends Authenticatable
{
    public function teams()
    {
        return $this->belongsToMany('\App\Team', 'associations')->using('App\Association');
    }
}

// App/Association
use Illuminate\Database\Eloquent\Relations\MorphPivot;

class Association extends MorphPivot
{
    protected $table = 'associations';  // not sure if this is needed
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-17
    • 1970-01-01
    • 2023-02-05
    • 2018-09-23
    • 1970-01-01
    • 2017-07-22
    相关资源
    最近更新 更多