如何使用Eloquent过滤数据透视表?

Ard*_*rda 29 filtering pivot-table laravel eloquent laravel-4

我正在使用我正在使用的项目上的数据透视表来获取用户的作品.

例如:User::find(1)->works给我ID为1的用户的作品.

问题是我想用额外的Pivot数据过滤这个结果.

就像是:

User::find(1)->works->pivot->where('active',1)->get();
Run Code Online (Sandbox Code Playgroud)

其中active是我在user_works数据透视表中设置的列.

这是我的User.php模型的相关部分:

<?php

class User extends Cartalyst\Sentry\Users\Eloquent\User {

    public function works() {
        return $this->belongsToMany('Work','user_works')->withPivot('active')->withTimestamps();
    }

}
Run Code Online (Sandbox Code Playgroud)

这是我的Work.php模型的相关部分:

<?php

class Work extends Eloquent {

    public function users() {
        return $this->belongsToMany('User','user_works')->withPivot('active')->withTimestamps();
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的数据透视表模式:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class CreateUserWorksTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('user_works', function(Blueprint $table) {
            $table->increments('id');

            $table->integer('user_id')->unsigned()->default(0);
            $table->integer('work_id')->unsigned()->default(0);

            $table->enum('active',array('0','1'))->default(1);

            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->foreign('work_id')->references('id')->on('works')->onDelete('cascade');

            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('user_works');
    }

}
Run Code Online (Sandbox Code Playgroud)

有没有办法在不为数据透视表制作新模型的情况下获取数据?

提前致谢,

编辑: 我可以这样过滤:

return User::find(1)->works()->where('user_works.active','=','1')->get();
Run Code Online (Sandbox Code Playgroud)

我不得不输入表名raw.但有没有更好的方法来获得这个而不使用它?

Ard*_*rda 41

Laravel 4.1带来了原生wherePivotorWherePivot 方法,这直接解决了我的问题.


vFr*_*sop 5

每当你调用withPivot('foo')Laravel 时,你都会:

SELECT ... `table`.`foo` AS `pivot_foo` FROM `table` ...
Run Code Online (Sandbox Code Playgroud)

固定答案:

HAVINGMySQL 特别允许在, GROUP BYand子句上使用列别名ORDER BY,但不允许在 onWHERE子句上使用。

HAVINGWHERE用于过滤查询,但它们的行为略有不同:HAVING应用于之后GROUP BYWHERE之前。

作为一般 SQL 规则,您不应该使用列别名(pivot_foo在这种情况下)进行分组、过滤或类似操作,因为它可能不适用于其他 SQL 数据库。

虽然不推荐,但可以使用:

return User::find(1)->works()->having('pivot_active','=','1')->get();
Run Code Online (Sandbox Code Playgroud)


小智 5

我尝试在两个方向上设置所有关系,因为这允许使用动态属性,例如$user->works().

class Collection extends Eloquent {
    public function contents()
    {
        return $this->belongsToMany('Content', 'collection_content', 'collection_id', 'content_id')->withPivot('collection_id', 'group_id', 'field_identifier');
    }
}

class Content extends Eloquent {
    public function collections()
    {
        return $this->belongsToMany('Collection', 'collection_content', 'collection_id', 'content_id')->withPivot('collection_id', 'group_id', 'field_identifier');
    }
}

class CollectionContent extends Eloquent {
    public function content()
    {
        return $this->belongsTo('Content');
    }

    public function collection()
    {
        return $this->belongsTo('Collection');
    }
}
Run Code Online (Sandbox Code Playgroud)

然后查询:

$works = User::find(1)->works()->where('active', 1)->get();
Run Code Online (Sandbox Code Playgroud)

当涉及到数据透视表的使用时,Eloquent 的文档很糟糕。这是一个很棒的教程:http : //www.develop.be/2013/08/30/laravel-4-pivot-table-example-attach-and-detach/