从 laravel5 中的模型和相关模型中搜索

Searching from model and related model in laravel5

我想在 Laravel5 中使用 Elequent 从 table 及其所有相关的 table 进行关键字搜索。 我的控制器是

 $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);

客户端模型,

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

客户地址信息,

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

如何在此处应用关键字搜索?

您可以使用whereHas按相关型号筛选:

$clients->whereHas('addressInfo', function($query) use ($q){
    $query->where("email", 'LIKE', "%$q%");
});