Answer the question
In order to leave comments, you need to log in
Laravel search in main model and related table?
Hello, I have a model User
, it has a field email
, it also has a related model Profile
, it has fields name, surname, nickname
. How to search these 4 fields using like ? That is, there is 1 input parameter and it is necessary to search in 4 fields. I tried to write the code (below), but it does not work, in principle, I understand why it does not work, but I do not quite know how to solve the problem.
$users = $users->where('email', 'like', "%{$search}%")->with(['profile' => function($q) use ($search) {
$q->orWhere('name', 'like', "%{$search}%");
$q->orWhere('surname', 'like', "%{$search}%");
$q->orWhere('nickname', 'like', "%{$search}%");
}]);
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question