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


返回

从User,User_Editor,User_Editor_Credentials,User_Editor_Specialty中选择*,其中User_Editor_Specialty.specialty输入(数组)。

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

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


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

最佳答案

对于那些可能长期遭受苦难试图找到解决嵌套关系的方法的人,并且,如果您正在编写带有whereIn的联接条件(由于Laravel中的错误,它会引发对未定义方法的调用),请在下面找到回答

    $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();

关于php - 有约束的 Eloquent 多重嵌套关系,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24656314/

10-10 18:08