如何使用Eloquent过滤数据透视表? [英] How to filter a pivot table using Eloquent?
问题描述
例如: 我正在使用我正在使用的项目中的数据透视表。 find(1) - > works 用户名::
给我ID为1的用户的作品。
事情是我想过滤这个
User :: find(1) - > work-> pivot-> where('active',1) - > get();
其中活动是我在我的user_works数据透视表中设置的列。
这是我的User.php模型的相关部分:
<?php
类用户扩展Cartalyst\Sentry\Users\Eloquent\User {
public function works(){
return $ this-> belongsToMany('Work','user_works') - > withPivot('active') - > withTimestamps();
}
}
这是我的相关部分Work.php模型:
<?php
class工作扩展Eloquent {
public function users(){
return $ this-> belongsToMany('User','user_works') - > withPivot('active') - > withTimestamps ();
}
}
这是我的数据透视表架构:
<?php
使用Illuminate\Database\Migrations\Migration ;
使用Illuminate\Database\Schema\Blueprint;
class CreateUserWorksTable扩展迁移{
/ **
*运行迁移。
*
* @return void
* /
public function up()
{
Schema :: create('user_works',function(Blueprint $表){
$ table-> increment('id');
$ table-> integer('user_id') - > unsigned() - > );
$ table-> integer('work_id') - > unsigned() - > default(0);
$ table->枚举('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 void
* /
public function down()
{
Schema :: drop('user_works');
}
}
有没有办法获得数据没有为数据透视表创建新的模型?
提前感谢
编辑:
我可以这样过滤:
return User :: find (1) - >作品() - 化合物其中( 'user_works.active', '=', '1') - >得到();
我不得不键入表名原始。但是,有没有更好的方式获得这个没有使用它?
Laravel 4.1将本机 wherePivot
和 orWherePivot
方法,这直接解决了我的问题。
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 Cartalyst\Sentry\Users\Eloquent\User {
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 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');
}
}
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屋!