雄辩的多个嵌套关系与约束

时间:2014-07-09 14:23:03

标签: php mysql laravel

尝试使用where约束从多个嵌套关系中获取数据:

模特用户:

    <?php

    use Illuminate\Auth\UserInterface;
    use Illuminate\Auth\Reminders\RemindableInterface;
    use Illuminate\Database\Eloquent\SoftDeletingTrait;
    use Zizaco\Entrust\HasRole;

  class User extends BaseModel implements UserInterface, RemindableInterface {
  use HasRole;

protected $fillable = array('username', 'password');
/**
 * The database table used by the model.
 *
 * @var string
 */
protected $table = 'users';

/**
 * The attributes excluded from the model's JSON form.
 *
 * @var array
 */
protected $hidden = array('password');
protected $dates = ['deleted_at'];
protected $softDelete = true;  

 public function editor()
{
    return $this->hasOne('User_Editor', 'user_id');
}
?>

Model User_Editor:

   <?php

   class User_Editor extends BaseModel {
/**
 * The database table used by the model.
 *
 * @var string
 */
protected $table = 'users_editors';

/**
 * The attributes excluded from the model's JSON form.
 *
 * @var array
 */
protected $hidden = array();

/**
 * Defiens the column names of fillable columns.
 *
 * @var array
 */
protected $fillable = array();

/**
 * Relationships
 */
public function credentials()
{
    return $this->hasMany('User_Editor_Credential', 'user_editor_id');
}

public function specialties()
{
    return $this->hasMany('User_Editor_Specialty', 'user_editor_id');
}
?> 

Model User_Editor_Credentials:

    <?php

 class User_Editor_Credential extends BaseModel {
/**
 * The database table used by the model.
 *
 * @var string
 */
protected $table = 'users_editors_credentials';

/**
 * The attributes excluded from the model's JSON form.
 *
 * @var array
 */
protected $hidden = array();

/**
 * Defiens the column names of fillable columns.
 *
 * @var array
 */
protected $fillable = array();
 }

Model User_Editor_Specialties:

    <?php

     class User_Editor_Specialty extends BaseModel {
/**
 * The database table used by the model.
 *
 * @var string
 */
protected $table = 'users_editors_specialties';

/**
 * The attributes excluded from the model's JSON form.
 *
 * @var array
 */
protected $hidden = array();

/**
 * Defiens the column names of fillable columns.
 *
 * @var array
 */
protected $fillable = array();
 }

返回

select * from User,User_Editor,User_Editor_Credentials,User_Editor_Specialty where User_Editor_Specialty.specialty In(array)。

到目前为止,我已尝试过,

    $this->data['editors'] = User::with(['editor.credentials.specialties' => function($q) use($data){
            $q->whereIn('specialty',$data);
        }])
        ->get();

但这会引发对未定义方法专业的错误调用。请指导,谢谢。

2 个答案:

答案 0 :(得分:1)

对于那些可能长期试图找到解决嵌套关系的方法的人,以及如果你用whereIn编写连接条件(由于Laravel中的错误而调用未定义的方法),请在下面找到ans,

    $editors = User::with(['editor.credentials','editor.specialties']);
    $this->data['editors'] = $editors->whereHas('editor', function($q) use ($a_data){
       $q->whereHas('specialties',function($sq) use($a_data){
            $sq->whereIn('specialty',$a_data);
       });
    })->get();

答案 1 :(得分:0)

更新:PR刚刚合并到4.2,所以现在可以在has方法中使用点嵌套表示法(->has('relation1.relation2) ->whereHas('relation1.relation2, ..

您的点标记关系必须在逻辑上链接:

`with(['editor.credentials', ' editor.specialties' => function ....

您试图在specialties型号上搜索User_Editor_Credential关系。


根据评论:

User::with(['editor.credentials','editor.specialties'])
    ->whereHas('editor' => function($q) use ($data){
       $q->whereHas('specialties' => function($q) use ($data){
          $q->whereIn('specialty',$data);
       });
    })->get();

或者如果您使用我的公关https://github.com/laravel/framework/pull/4954

User::with(['editor.credentials','editor.specialties'])
    ->whereHas('editor.specialties' => function($q) use ($data){
        $q->whereIn('specialty',$data);
    })->get();

它将返回与users相关的editor.specialties匹配的所有whereIn,以及相关的editor及其相关credentialsspecialties(后者不会被过滤掉)