James James - 3 months ago 9
PHP Question

Retrieve all users based on where condition of relationship

I'm trying to build a search system for my users.

The first part of this was easy as all the information I was searching was in the same table.

The second part involves searching for users based upon a provided IP address. For this I am using:

$users = User::with(array('login' => function($q){
$q->where('ip_address', Input::get('ipaddress'));
}))->paginate(10);


However this is returning all users, rather than just those that meet the IP address requirements. Where am I going wrong? Or is there an easier way to do this?

Answer
$users = User::whereHas('login', function ($q) {
    $q -> where('ip_address', Input::get('ipaddress'));
}) -> paginate(10);

Try it!

Comments