0

I want to keyword search from a table and its all related table using Elequent in Laravel5. My controller is

 $clients = Client::with('contacts', 'paymentTerm', 'addressInfo');
    if ($q = Request::input("q")) {
        $clients->where("clients.name", 'LIKE', "%$q%");
        $clients->where("address_info.email", 'LIKE', "%$q%");//This is not working,I want to search from both client and client address_info
    }
    return $clients->paginate(10);

Client Model ,

 public function addressInfo() {
    return $this->hasOne('App\Model\ClientAddressInfo');
}

Client Address info,

public function client() {
        return $this->belongsTo('App\Model\Client');
    }

how can I apply keyword search here?

gsk
  • 2,329
  • 8
  • 32
  • 56

1 Answers1

2

You can use whereHas to filter by a related model:

$clients->whereHas('addressInfo', function($query) use ($q){
    $query->where("email", 'LIKE', "%$q%");
});
lukasgeiter
  • 147,337
  • 26
  • 332
  • 270