首页 文章

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

提问于
浏览
27

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

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

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

就像是:

User::find(1)->works->pivot->where('active',1)->get();

其中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();
    }

}

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

<?php

class Work extends Eloquent {

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

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

<?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');
    }

}

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

提前致谢,

Edit: 我可以这样过滤:

return User::find(1)->works()->where('user_works.active','=','1')->get();

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

3 回答

  • 1

    Laravel 4.1带来原生 wherePivotorWherePivot methods,它直接解决了我的问题 .

  • 38

    每当你打电话给 withPivot('foo') 时,Laravel你会:

    SELECT ... `table`.`foo` AS `pivot_foo` FROM `table` ...
    

    修正答案:

    MySQL特别允许在 HAVINGGROUP BYORDER BY 子句中使用列别名,但不允许在 WHERE 子句中使用列别名 .

    HAVINGWHERE 都用于过滤查询,但它们的行为略有不同: HAVINGGROUP BY 之后应用, WHERE 在之前 .

    作为一般的SQL规则,您不应该使用列别名(在这种情况下为 pivot_foo )进行分组,过滤或类似的任何操作,因为它可能无法与其他SQL数据库一起使用 .

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

    return User::find(1)->works()->having('pivot_active','=','1')->get();
    
  • 2

    我尝试在两个方向上设置所有关系,因为这允许使用动态属性,例如$ 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');
        }
    }
    

    然后查询:$ works = User :: find(1) - > works() - > where('active',1) - > get();

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

相关问题