Chron
2 weeks ago
162
5
Laravel

Is there a way to eager load models with "has"?

Posted 2 weeks ago by Chron

Is there a way to achieve this?

Model1::has(['models2' => function ($q) use ($userId) {
	$q->where('user_id', $userId);
}])->get();

For example, the userId is 4, I'll only get the records of Model1 with Model2 that has the userId of 4.

Please sign in or create an account to participate in this conversation.