我希望在Laravel5中使用Elequent从表及其所有相关表中进行关键字搜索。 我的控制器是
$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');
}
如何在此处应用关键字搜索?
答案 0 :(得分:2)
您可以使用whereHas
按相关模型进行过滤:
$clients->whereHas('addressInfo', function($query) use ($q){
$query->where("email", 'LIKE', "%$q%");
});