integrasolid
3 years ago

Eloquent conditional query on relationship table

Posted 3 years ago by integrasolid

Hi I need help on this one, lets say I'm building a USER LIST HTML table and need a search function

Code in User Controller:

public function index()
{
    $users = User::with('user_info')->filter()->paginate(5);
    dd($users);
}

Code in User model :

public function user_info()
{
    return $this->hasOne('User_info','user_id','id');    
}

public function filter()
{
  $candidate_name = Input::get('candidate_name'); 
  if (!empty($candidate_name)) {
    $this->where('user_info.candidate_name', '=', $candidate_name);
  }  
  return $this;
}

However I get this error:

Call to undefined method Illuminate\Database\Query\Builder::filter()

So my question is how do I properly called filter() method in user model so I can apply conditional where to?

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