amarsyla
4 years ago
637
11
Eloquent

Merging two complex Eloquent queries

Posted 4 years ago by amarsyla

Hey guys,

I have this query:

$query = Connection::where('name', 'LIKE', '%' . $request->input('name') . '%')

->where('name',     'LIKE', '%' . $request->input('name')      . '%')
->where('email',    'LIKE', '%' . $request->input('email')     . '%')
->where('phone',    'LIKE', '%' . $request->input('phone')     . '%')
->where('company',  'LIKE', '%' . $request->input('$company')  . '%')
->where('function', 'LIKE', '%' . $request->input('function')  . '%')
->where('social',   'LIKE', '%' . $request->input('social')    . '%')
->where('address',  'LIKE', '%' . $request->input('$address')  . '%')
->where('city',     'LIKE', '%' . $request->input('city')      . '%')
->where('postcode', 'LIKE', '%' . $request->input('postcode')  . '%');

if($request->has('gender')) {
    $query->where('gender', $request->input('gender'));
}

if($request->has('birthday')) {
    $query->where('birthday', $request->input('birthday'));
}

I fetch it with this code:

$search_results = $query->paginate(12);

Now, I want that query duplicated, but instead of the Connection model, I want to use the User model. So, I can search both of them. How can I merge those two queries, and just as I fetched one model, now fetch both of them and paginate?

Thanks.

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