James James - 1 year ago 50
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'));

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 Source
$users = User::whereHas('login', function ($q) {
    $q -> where('ip_address', Input::get('ipaddress'));
}) -> paginate(10);

Try it!