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

时间:2015-03-08 15:14:10

标签: php mysql laravel laravel-5

我希望在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');
    }

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

1 个答案:

答案 0 :(得分:2)

您可以使用whereHas按相关模型进行过滤:

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