简体   繁体   English

Laravel - 如何使用关系列中的任何内容创建搜索过滤器

[英]Laravel - how to make a search filter with like anything in relationship columns

I'm trying to make a query where returns me the results, based on a request word, but I have to filter with the relationship columns as well.我正在尝试根据请求词进行查询,将结果返回给我,但我也必须使用关系列进行过滤。 Example:例子:

Payment::byUnity($unityId)
            ->with(['contract:id,nome,sigla'])
            ->latest('id')
            ->where('id', 'LIKE', '%' . $request->input('search') . '%')
            ->orWhere('name', 'LIKE', '%' . $request->input('search') . '%')
            ->orWhere('contract.name', 'LIKE', '%' . $request->input('search') . '%')
            ->orWhere('contract.description', 'LIKE', '%' . $request->input('search') . '%')
            ->paginate(15)

How can I filter the "contract.name" on the query?如何过滤查询中的“contract.name”? Because the way of the example returns me error.因为示例的方式返回错误。 Thanks in advance!提前致谢!

You can do你可以做

Payment::byUnity($unityId)
            ->with(['contract:id,nome,sigla'])
            ->latest('id')
            ->where('id', 'LIKE', '%' . $request->input('search') . '%')
            ->orWhere('name', 'LIKE', '%' . $request->input('search') . '%')
            ->orWhereHas('contract', function($query) use ($request) {
                return $query->where('name', 'LIKE', '%' . $request->input('search') . '%')
                ->orWhere('description', 'LIKE', '%' . $request->input('search') . '%');
            }
            ->paginate(15)

NOTE: You must have proper relationships set up for this query to work.注意:您必须设置适当的关系才能使此查询起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM