[英]Searching from model and related model in laravel5
I want to keyword search from a table and its all related table using Elequent in Laravel5. 我想在Laravel5中使用Elequent从表及其所有相关表中进行关键字搜索。 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? 如何在此处应用关键字搜索?
You can use whereHas
to filter by a related model: 您可以使用whereHas
按相关模型进行过滤:
$clients->whereHas('addressInfo', function($query) use ($q){
$query->where("email", 'LIKE', "%$q%");
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.