Php 带约束的雄辩多重嵌套关系

Php 带约束的雄辩多重嵌套关系,php,mysql,laravel,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

尝试从具有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');
}
?>

但这会向未定义的方法特殊性抛出一个错误调用。请指导,谢谢。

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

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

`with(['editor.credentials', ' editor.specialties' => function ....
您试图在
User\u Editor\u 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();
或者如果你用我的公关


它将返回所有
用户
,这些用户具有与
匹配的
相关的编辑器.专业,其中
,以及相关的
编辑器
及其所有相关的
凭证
专业
(后者不会被过滤)

对于那些长期以来一直试图找到解决嵌套关系的方法的人,以及如果您正在使用where编写连接条件(由于Larvel中的错误而引发对undefined方法的调用),请查找下面的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();

谢谢,这很管用。但它提供了所有用户,而且似乎没有考虑where条件。我尝试运行该查询。它确实在users表上选择了*。我认为has()可能会提供所需的结果,但不确定如何处理has()对于与where子句的多个嵌套关系。那么您希望使用该
where
子句过滤什么以及您希望返回什么?如果specialties表中的Speciality与$data数组中的一个值匹配,我需要从users表、Editor表、credentials表中获取所有数据。因此,用户的id=1 username='admin'id=2 username='testeditor',Users\u Editor有id=1 user\u id(FK)=2 self\u description=“some stuff”,user\u Editor\u凭证有id=1 user\u Editor\u id=1(FK)credential=“testcredentials”,user\u Editor\u Specialties有id=1 user\u Editor\u id=1(FK)specialty=“Books”。因此,如果specialty=“Books”,我想从所有表中获取所有数据过滤结果将是与“testeditor”相关的详细信息,whereHas()不适用于点表示法。我将需要使用联接,但同样,如果我使用join,就像我在join中使用WHERE()子句一样,它将调用未定义的方法JoinClause::WHERE()你是对的,但我在laravel回购协议上的PR允许点嵌套在
whereHas
:。同时我添加了代码如何做到这一点。
    $this->data['editors'] = User::with(['editor.credentials.specialties' => function($q) use($data){
            $q->whereIn('specialty',$data);
        }])
        ->get();
`with(['editor.credentials', ' editor.specialties' => function ....
User::with(['editor.credentials','editor.specialties'])
    ->whereHas('editor' => function($q) use ($data){
       $q->whereHas('specialties' => function($q) use ($data){
          $q->whereIn('specialty',$data);
       });
    })->get();
User::with(['editor.credentials','editor.specialties'])
    ->whereHas('editor.specialties' => function($q) use ($data){
        $q->whereIn('specialty',$data);
    })->get();
    $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();