【问题标题】:Eloquent BelongsToManyThroughEloquent BelongsToMany through
【发布时间】:2020-04-25 10:58:19
【问题描述】:
    User belongsToMany Project 

    Project belongsTo Account

   wanted: Account belongsToMany User Through Project

项目和用户之间存在订阅枢轴模型。

我想要:

$user->accounts

更新:

我想关系会得到$user->projects,然后得到accounts

$user->projects()->accounts()

【问题讨论】:

  • $user->projects()->acounta()
  • $user->projects() 返回project_user的数据透视表集合
  • 你能分享一下你与问题相关的表结构吗?

标签: laravel eloquent


【解决方案1】:

如果您的结构允许,您可以使用hasManyThrough

class User extends Model
{
    ...

    public function accounts()
    {
        return $this->hasManyThrough(
            'App\Account',
            'App\Project',
        );
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-10
    • 2017-02-04
    • 2014-05-06
    • 1970-01-01
    相关资源
    最近更新 更多