【问题标题】:Counting eloquent relations from pivot table in L4从 L4 中的数据透视表计算 eloquent 关系
【发布时间】:2015-03-18 22:58:09
【问题描述】:

我有以下表格:

用户

    Schema::create('users', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('username', 30);
        $table->string('email')->unique();
        $table->string('password', 60);
        $table->string('remember_token')->nullable();
        $table->timestamps();
    });

组织

    Schema::create('organisations', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('name')->unique('name');
        $table->integer('owner_id')->unsigned()->index()->nullable();
        $table->foreign('owner_id')->references('id')->on('users');
        $table->timestamps();
    });

这是我的 organisation_user 数据透视表:

public function up()
{
    Schema::create('organisation_user', function(Blueprint $table)
    {
        $table->increments('id');
        $table->integer('organisation_id')->unsigned()->index();
        $table->foreign('organisation_id')->references('id')->on('organisations')->onDelete('cascade');
        $table->integer('staff_id')->unsigned()->index();
        $table->foreign('staff_id')->references('id')->on('users')->onDelete('cascade');
    });
}

我的模型的规则是:

  • 一个组织属于一个用户(所有者) - 并非总是如此,即nullable owner_id
  • 一个组织下可能有很多用户(员工)

因此,我的Organisation eloquent 模型看起来像这样:

class Organisation extends Eloquent {

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function owner()
    {
        return $this->belongsTo('User', 'owner_id', 'id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function staffs()
    {
        return $this->hasMany('User', 'staff_id', 'id');
    }

}

这就是我在控制器中加载模型并将其传递给视图的方式:

public function index()
{
    return View::make('organisations.index')
        ->with('organisations', Organisation::with('owner', 'staffs')->get());
}

在我看来,我这样显示数据:

@foreach($organisations as $organisation)
    <div>
        Name : {{  $organisation->name }}
        <br>
        Owner: {{ $organisation->owner->email }}
        <br>
        Staffs: {{ $organisation->staffs->count() }}
    </div>
@endofreach

执行上述操作时,出现以下错误:

SQLSTATE[42S22]: 找不到列: 1054 未知列 'where 子句'中的'users.staff_id' (SQL: select * from users where users.staff_id in (1))

知道为什么我在这里做错了吗?您如何正确地将关系与预加载联系起来?

我需要一个单独的数据透视表模型才能工作吗?

【问题讨论】:

    标签: laravel laravel-4 relational-database eloquent eager-loading


    【解决方案1】:

    多对多关系使用belongsToMany() 方法,而不是hasMany() 方法。

    更新您的代码:

    class User extends Eloquent
    {
        public function staffs()
        {
            return $this->belongsToMany('Organisation', 'organisation_user', 'staff_id','organisation_id');
        }
    }
    

    也在视图中,试试这个 Staffs: {{ $organisation-&gt;staffs()-&gt;count() }}

    请注意,唯一的变化是向员工添加了(),我自己无法测试此代码,但据我所知,-&gt;staffs 方法将返回所有相关模型的Eloquent\Collection (Users)并且() 将返回您在模型中的关系方法中定义的hasMany() 对象,与Eloquent\Collection 相比,它具有其他功能

    仔细检查关于多对多关系的 Eloquent 文档。

    【讨论】:

    • 感谢您的澄清。我不再收到任何错误,但结果现在是0。我在数据透视表中有数据。有什么想法吗?
    • 我会更新答案,看这个空间刮一下,你是如何在查询时加载关系的?
    • 我的所有代码都发布在问题中。我假设通过定义关系,Eloquent 提出了正确的查询?我错过了一步吗?
    • 再次更新答案
    【解决方案2】:

    在我看来 staffs 实际上是 many-to-many relationship。这意味着你需要belongsToMany()

    public function staffs()
    {
        return $this->belongsToMany('User', 'organisation_user', 'organisation_id', 'staff_id');
    }
    

    【讨论】:

    • 感谢您的澄清。我不再收到任何错误,但结果现在是0。我在数据透视表中有数据。有什么想法吗?
    • 它生成的查询是:SELECT organisations.*, organisation_user.staff_id` as pivot_staff_id, organisation_user.organisation_id as pivot_organisation_id FROM @inner join @9876543 987654334@ on organisations.id = organisation_user.organisation_id WHERE organisation_user.staff_id in ('1')` 看起来不正确。
    • 貌似外键切换了。你喜欢我的回答吗?
    猜你喜欢
    • 2020-08-22
    • 2019-01-25
    • 2023-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-07
    相关资源
    最近更新 更多