如何在Laravel中通过复合标准进行过滤?

50pmv0ei  于 2023-03-31  发布在  其他
关注(0)|答案(3)|浏览(107)

所以我有代码,可以搜索数据的基础上关系列,有2个搜索输入,其中一个工作得很好,但另一个不工作。
我的模特
分类

  • 身份证
  • 名称

任务

  • 身份证
  • 标题
  • 类别标识
  • 公司标识

公司

  • 身份证
  • 名称

这是我的剑

<form action="/admin/manage" method="get">

     <!--1. this is search name -->
    <input name="search_name">

     <!--2. this is company_id search -->
    <select name="company_id" >
             <option value="">Select All</option>
              @foreach($companies as $company)
                <option value="{{ $company->id }}" >{{ $company->name }}</option>
              @endforeach
    </select>
</form>

我的控制器:

if($request->search_name || $request->company_id){
            $categories = Category::with(['tasks' => fn($query) => $query->where('title', 'like', '%' . $request->search_name . '%')])
                        ->whereHas('tasks', fn ($query) => 
                        $query->where('title', 'like', '%' . $request->search_name . '%')
                        )->get();
}
else{
    $categories = Category::with('tasks')->get();
}

这段代码似乎未完成,它只是搜索标题在我的任务,我不知道如何添加更多的代码,可以搜索2列在一起

w8ntj3qf

w8ntj3qf1#

因为你使用Laravel 8,你不能使用withWhereHas()。所以你可以在你的类中定义一个函数并重用它:

function filterTasks($query, $request)
{
    if ($request->search_name) {
        $query->where('title', 'like', '%' . $request->search_name . '%');
    }

    if ($request->company_id) {
        $query->where('company_id', $request->company_id);
    }
}

然后:

$categories = Category::whereHas('tasks', function ($query) use ($request) {
    $this->filterTasks($query, $request);
})->with([
    'tasks' =>function ($query) use ($request) {
        $this->filterTasks($query, $request);
    }
])->get();

如果你使用Laravel 9或更高版本,你应该使用withWhereHas()。该方法应用了即时加载,同时检查了条件。它使你的代码更短,更干净,并且没有重复的代码。

$categories = Category::withWhereHas('tasks', function ($query) use ($request) {
    if ($request->search_name) {
        $query->where('title', 'like', '%' . $request->search_name . '%');
    }

    if ($request->company_id) {
        $query->where('company_id', $request->company_id);
    }
})->get();
h22fl7wq

h22fl7wq2#

您可以尝试从两个值中搜索:

if($request->search_name || $request->company_id){
    $searchName = $request->search_name;
    $companyId = $request->has('company_id') ? $request->company_id : false;

    $categories = Category::with(['tasks'])
                    ->whereHas('tasks', function($query) use ($searchName, $companyId) {
                        $query->where('title', 'like', '%' . $searchName . '%');
                        if($companyId) {
                            $query->orWhere('company_id', $companyId);
                        }
                    })->get();
} else{
    $categories = Category::with('tasks')->get();
}
wkyowqbh

wkyowqbh3#

您可以链接where,以便生成的查询最终将具有LIKEAND an =条件:

if($request->search_name || $request->company_id){
            $categories = Category::with(['tasks' => fn($query) => $query->where('title', 'like', '%' . $request->search_name . '%')])
                        ->whereHas('tasks', fn ($query) => 
                        //search_name might be empty, in which case we do not want to compute a costly wildcard comparison that all records will fulfill as long as they are not null
                        if ($request->search_name) {
                            $query->where('title', 'like', '%' . $request->search_name . '%')
                        }
                        //We check whether we need a criteria for company_id
                        if ($request->company_id) {
                            $query->where('company_id', $companyId);
                        }
                        )->get();
}
else{
    $categories = Category::with('tasks')->get();
}

相关问题