具有约束的雄辩的多个嵌套关系


Eloquent multiple nested relations with constraint

尝试使用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');
}
?>

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

模型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();
 }

模型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。

到目前为止我已经试过了,

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

但是这会抛出对未定义方法特色化的错误调用。

对于那些可能长期试图找到一种方法来解决嵌套关系的人来说,如果你正在编写一个连接条件,其中(由于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();

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

点符号关系必须在逻辑上链接:

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

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


根据注释:

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

或者如果你使用我的PR 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。与whereIn、相关editor及其所有相关credentialsspecialties(后者不进行过滤)匹配的特长