如何使用 Eloquent 过滤数据透视表? [英] How to filter a pivot table using Eloquent?

查看:24
本文介绍了如何使用 Eloquent 过滤数据透视表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

类似于:

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

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

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

<?php类用户扩展 CartalystSentryUsersEloquentUser {公共功能作品(){return $this->belongsToMany('Work','user_works')->withPivot('active')->withTimestamps();}}

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

<?php类工作扩展雄辩{公共功能用户(){return $this->belongsToMany('User','user_works')->withPivot('active')->withTimestamps();}}

这是我的数据透视表架构:

<?php使用 IlluminateDatabaseMigrationsMigration;使用 IlluminateDatabaseSchemaBlueprint;类 CreateUserWorksTable 扩展迁移 {/*** 运行迁移.** @return 无效*/公共函数 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();});}/*** 反转迁移.** @return 无效*/公共函数 down(){架构::drop('user_works');}}

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

提前致谢,

我可以这样过滤:

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

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

解决方案

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

I'm using a pivot table on the project I'm working with to get works of users.

E.g: User::find(1)->works gives me the works of user with ID of 1.

The thing is that I want to filter this results with extra Pivot data.

Something like:

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

In which the active is the column I've set in my user_works pivot table.

This is my related part of my User.php model:

<?php

class User extends CartalystSentryUsersEloquentUser {

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

}

This is my related part of my Work.php model:

<?php

class Work extends Eloquent {

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

This is my pivot table schema:

<?php

use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;

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

}

Is there any way to gain the data without making a new model for the pivot table?

Thanks in advance,

Edit: I can filter this way:

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

I had to type table name raw. But is there a better way to gain this without using it?

解决方案

Laravel 4.1 brings native wherePivot and orWherePivot methods, which is directly a solution to my problem.

这篇关于如何使用 Eloquent 过滤数据透视表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆