Laravel 5雄辩HasManyThrough - 链接中的名称列 [英] Laravel 5 Eloquent HasManyThrough - name columns in links

查看:119
本文介绍了Laravel 5雄辩HasManyThrough - 链接中的名称列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:在看到有说服力的ManyThrough课程之后,不改变它,我看不到如何链接下面的表。方法调用的参数声明是:

UPDATE: After looking at the Eloquent hasManyThrough class, without changing it, I cannot see how I could link the tables below. The declarations of params for the method call is:

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) - > organisation_users_roles(organisation_id)
,第二个链接是 organisation_users_roles(user_id) - > users(id)

So there is no way to tell the standard Eloquent hasManyThrough that first link is organisation(id)->organisation_users_roles(organisation_id) and second link is organisation_users_roles(user_id)->users(id)

我误会了吗?

如果您有三个表通过 hasManyThrough 链接,我找不到要使用的语法示例每个表中的列名称不符合雄辩的命名约定。在我的情况下:

I cannot find an example of the syntax to use if you have three tables linked via hasManyThrough and the column names in each table do not follow the Eloquent naming convention. In my case:

三个表:

Organisations
Users
Roles
Organisation_Users_Roles (3 way linking)

因为这些属于已存在的应用程序,我无法更改/重命名列。

Because these belong to an already existing app, I cannot change / rename columns.

链接列名称为:

`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"

Organisation_Users_Roles 还包含一个 id 列(基本上是其主键或行ID)。

The Organisation_Users_Roles also contains an id column (essentially its primary key or row id).

我已经尝试了(例如)组织 class:

I have tried (as example) in Organisation class:

public function users()
{
    return $this->hasManyThrough(User::class, OrganisationUserRole::class);
}

在文档示例中,表格为:

In the docs example the tables are:

countries
    id - integer
    name - string

users
    id - integer
    country_id - integer
    name - string

posts
    id - integer
    user_id - integer
    title - string

所以,在例子中:

country(id) - > users(country_id)

users(id) - > posts(user_id)

在我的情况下:

organization(id) - > organisation_users_roles(organisation_id)

organisation_users_roles(user_id) - > users(id)

但由于列名称不完全相当于Eloquent Docs的示例(因为这是一种三向链接表),所以使用以下方法时我会收到以下错误:

But since the column names are not exact to the example on Eloquent Docs (as this is a three way linking table) then I get the following error when using the method:

[PDOException]
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.user_role_id' in 'on clause'

我想要得到e首先返回当前组织的用户,我称之为:

I want to get the first returned user for the current Organisation, I call it like this:

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

谢谢

推荐答案

在您的组织表中,尝试

In your organisation table, try

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

这篇关于Laravel 5雄辩HasManyThrough - 链接中的名称列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆