Mysql Laravel 5 Elotent在链接中有许多名称列

Mysql Laravel 5 Elotent在链接中有许多名称列,mysql,eloquent,laravel-5.1,Mysql,Eloquent,Laravel 5.1,更新:在看了雄辩的hasManyThrough类之后,如果不修改它,我看不出如何链接下面的表。方法调用的参数声明为: class HasManyThrough extends Relation { /** * The distance parent model instance. * * @var \Illuminate\Database\Eloquent\Model */ protected $farParent; // in my c

更新:在看了雄辩的hasManyThrough类之后,如果不修改它,我看不出如何链接下面的表。方法调用的参数声明为:

class HasManyThrough extends Relation
{
    /**
     * The distance parent model instance.
     *
     * @var \Illuminate\Database\Eloquent\Model
     */
    protected $farParent;  // in my case: User::class

    /**
     * The near key on the relationship.
     *
     * @var string
     */
    protected $firstKey;  // in my case: organisation_users_roles(organisation_id)

    /**
     * The far key on the relationship.
     *
     * @var string
     */
    protected $secondKey;  // in my case: users(id)

    /**
     * The local key on the relationship.
     *
     * @var string
     */
    protected $localKey;  // in my case: organisation(id)
因此,没有办法告诉标准的雄辩的hasManyThrough第一个链接是
organization(id)->organization\u users\u roles(organization\u id)
第二个链接是
organization\u users\u roles(user\u id)->users(id)

我误解了吗


如果您有三个表通过
hasManyThrough
链接,并且每个表中的列名不遵循雄辩的命名约定,那么我找不到语法示例。就我而言:

三张表:

Organisations
Users
Roles
Organisation_Users_Roles (3 way linking)
因为它们属于已经存在的应用程序,所以我无法更改/重命名列

链接列名为:

`Organisation: id` links to `Organisation_Users_Roles: organisation_id"
`Users: id` links to `Organisation_Users_Roles: user_id"
`Roles: id` links to `Roles: role_id"
organization\u Users\u Roles
还包含一个
id
列(基本上是它的主键或行id)

我在
组织
课程中尝试过(例如):

public function users()
{
    return $this->hasManyThrough(User::class, OrganisationUserRole::class);
}
在文档示例中,表格如下所示:

countries
    id - integer
    name - string

users
    id - integer
    country_id - integer
    name - string

posts
    id - integer
    user_id - integer
    title - string
因此,在示例中:

country(id)->用户(country\u id)
users(id)->posts(user\u id)

就我而言:

organization(id)->organization\u users\u roles(organization\u id)
组织用户角色(用户id)->用户(id)

但是,由于列名与雄辩文档上的示例不完全相同(因为这是一个三向链接表),因此在使用该方法时,我得到以下错误:

[PDOException]
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.user_role_id' in 'on clause'
我想获得当前组织的第一个返回用户,我这样称呼它:

$entry['created_user_id'] = $organisation->users->first();

谢谢

在您的组织表中,请尝试

return $this->hasManyThrough('App\..path-to..\User', 'App\..path-to..\Organisation_Users_Roles', 'organisation_id', 'id' );

嗨,巴拉特,它不工作了。我得到
用户。找不到用户id
。嗨,巴拉特,这返回null,这是不正确的。查询正常,但两个表之间的链接构建不正确(
user\u role
id
=
users
id