【问题标题】:How to filter a pivot table using Eloquent?如何使用 Eloquent 过滤数据透视表?
【发布时间】:2013-09-03 12:21:36
【问题描述】:

我在我正在处理的项目中使用数据透视表来获取用户的作品。

例如: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');
    }

}

有没有什么方法可以在不为数据透视表创建新模型的情况下获取数据?

提前致谢,

编辑: 我可以这样过滤:

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

我必须输入原始表名。但是有没有更好的方法来获得它而不使用它?

【问题讨论】:

  • 您在编辑中提供的内容对我来说看起来很棒。 '=' 不是第二个参数,但 -&gt;where('user_works.active', 1) 应该可以正常工作。
  • @Deefour 好吧,还有其他参数,更大或更小等等。我只是复制粘贴。我想知道的是,我可以动态捕获表名,而不是让表名公开保护。这将是我需要放置原始表名的唯一地方,这感觉不对。

标签: laravel laravel-4 filtering eloquent pivot-table


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      每当你打电话给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();
      

      【讨论】:

      • 您说得对,先生。我从未在 where 子句中使用过它,根据 MySQL 文档,您只能在 groupBy、orderBy 和 have 上使用列别名。我已经确定了答案。
      • 太好了,这和我预期的一样。谢谢! (ps:我不是投反对票的人,但我投了赞成票)
      • @vFragshop 另外感谢您的详细说明,我目前将这两种​​解决方案都保存在我身边以防万一。
      • 抱歉,我不得不取消选择你的​​答案作为最佳答案,因为 Laravel 4.1 为我的问题带来了新的原生方法。
      猜你喜欢
      • 2017-09-30
      • 1970-01-01
      • 2017-10-17
      • 1970-01-01
      • 2016-12-24
      • 1970-01-01
      • 2023-02-24
      • 2021-01-09
      相关资源
      最近更新 更多