T
T
timehollyname2021-12-04 15:46:23
Laravel
timehollyname, 2021-12-04 15:46:23

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

1 answer(s)
J
jazzus, 2021-12-04
@timehollyname

orWhereHas

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question