计算L4中数据透视表的雄辩关系

时间:2015-01-20 16:10:49

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

我有以下表格:

用户

    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雄辩模型如下所示:

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其中usersstaff_id in( 1))

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

我是否需要一个单独的数据透视表模型才能实现此目的?

2 个答案:

答案 0 :(得分:2)

我认为staffs实际上是many-to-many relationship。这意味着您需要belongsToMany()

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

答案 1 :(得分:0)

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

更新您的代码:

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

同样在视图中,试试这个 Staffs: {{ $organisation->staffs()->count() }}

请注意,唯一的变化是向工作人员添加(),我无法自行测试此代码,但我记得->staffs方法会返回Eloquent\Collection个相关模型(Users)和()将返回您在模型中的关系方法中定义的hasMany()对象,该对象具有与Eloquent\Collection相比的其他功能

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

相关问题