你好,我是Laravel的初学者 我试图用雄辩的方式检索数据,但我必须添加一个与关系相关的条件 我的模型
class DemandeInscription extends Model
{
public function user(){
return $this->belongsTo('App\User','tuteur_id');
}
}
这是我在控制器中尝试过的内容
$demandes= DemandeInscription::with('user','meet','enfant')
->where('confirmation','=',0)->user()
->where('phone_verified_at','!=',null)
->orderBy('created_at','desc')->paginate(10);
我收到错误
Arguments
"Call to undefined method Illuminate\Database\Eloquent\Builder::user()"
答案 0 :(得分:1)
那么您可以通过这种方式获得它
$demandes= DemandeInscription::with(['user' => function($query){
$query->where('phone_verified_at','!=',null);
},'meet','enfant'])
->where('confirmation','=',0)
->orderBy('created_at','desc')->paginate(10);
OR
$demandes= DemandeInscription::whereHas(['user' => function($query){
$query->where('phone_verified_at','!=',null);
}])
->orwhereHas('meet','enfant')
->where('confirmation','=',0)
->orderBy('created_at','desc')->paginate(10);
在User Model
内使用“本地作用域”进行操作或编写此作用域
public function scopeVerified($query){
return $query->where('phone_verified_at','!=',null);
}
$demandes= DemandeInscription::with(['user' => function($query){
$query->verified();
},'meet','enfant'])
->where('confirmation','=',0)
->orderBy('created_at','desc')->paginate(10);