簡體   English   中英

Laravel - 如何使用關系列中的任何內容創建搜索過濾器

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

我正在嘗試根據請求詞進行查詢,將結果返回給我,但我也必須使用關系列進行過濾。 例子:

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)

如何過濾查詢中的“contract.name”? 因為示例的方式返回錯誤。 提前致謝!

你可以做

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)

注意:您必須設置適當的關系才能使此查詢起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM