如何使用 with 语句对关系 table 应用过滤器

How to apply filter with relational table using with statement

我写了一个代码来过滤关系数据,但我没有得到好的结果。这是代码。父查询和查询独立工作。

public function get(Request $request, Company $company, Survey $survey)
{
    $this->authorize('update', $survey->company);
    $search = $request->get('search');
    $query = EsAnswer::where(['company_id' => $company->id, 'survey_id' => $survey->id])
        ->orderByDesc('created_at')
        ->with(['employee'=> function ($q) use($search) {
            $q->where('first_name', 'LIKE', "%{$search}%");
            $q->orwhere('last_name', 'LIKE', "%{$search}%");
            $q->select('id', 'first_name', 'last_name');   
        }]);
    if ($request->has('start_date')) {
        $query->whereDate('created_at', '>=', $request->start_date);
    }
    if ($request->has('end_date')) {
        $query->whereDate('created_at', '<=', $request->end_date);
    }

    $answers = $query->get()->groupBy('submission_id');
        // ->paginate('100');
    return $answers;
}

我用这个的时候

{
    "search":""
}

我收到这样的回复:

{
    "id": 2,
    "company_id": 1,
    "employee_id": 1,
    "survey_id": 2,
    "es_question_id": 1,
    "answer": "done",
    "submission_id": "1",
    "mark_as_read": 1,
    "created_at": "2020-08-19 12:18:25",
    "updated_at": "2020-08-26 06:55:21",
    "employee": {
        "id": 1,
        "first_name": "Baseapp",
        "last_name": "saw"
    }
}

当我尝试按另一个 table 存在的员工姓名进行搜索时,如果数据存在,则应过滤数据并给出响应,但如果员工姓名或搜索数据不存在,则数据不应给予任何回应。如何处理?

{
    "search":"sure"
}


{
    "id": 2,
    "company_id": 1,
    "employee_id": 1,
    "survey_id": 2,
    "es_question_id": 1,
    "answer": "done",
    "submission_id": "1",
    "mark_as_read": 1,
    "created_at": "2020-08-19 12:18:25",
    "updated_at": "2020-08-26 06:55:21",
    "employee": null
}

此处数据为空,但显示父数据的原因。请帮助并提供另一个想法来过滤关系数据。

使用whereHas()函数

  $query = EsAnswer::with('employee:id,first_name,last_name')
        ->where(['company_id' => $company->id, 'survey_id' => $survey->id])
        ->whereHas('employee', function($query) use($search) {
            $query->where('first_name', 'LIKE', "%$search%")
                ->orwhere('last_name', 'LIKE', "%$search%");
        })
        ->orderByDesc('created_at');

ref link https://laravel.com/docs/7.x/eloquent-relationships#querying-relationship-existence

它使用 whereHas,然后使用 With :

$query = EsAnswer::where(['company_id' => $company->id, 'survey_id' => $survey->id])
    ->orderByDesc('created_at')
    ->whereHas('employee', function ($q) use($search) {
      return $q->where('first_name', 'LIKE', "%{$search}%")
      ->orwhere('last_name', 'LIKE', "%{$search}%");   
    })->with(['employee' => function ($q){
        $q->select('id', 'first_name', 'last_name');
    }]);

我们使用 whereHas 筛选相关的 table。所以像这样使用它

$query = EsAnswer::with('employee:id,first_name,last_name')
        ->where(['company_id' => $company->id, 'survey_id' => $survey->id])
        ->whereHas('employee', function($query) use($search) {
            $query->where('first_name', 'LIKE', "%{$search}%")
                ->orwhere('last_name', 'LIKE', "%{$search}%");
        })
        ->orderByDesc('created_at');

这将在您的相关 table 和 return 中搜索 null 如果没有找到任何内容。我已经为 eager load 数据添加了优雅的方式。你也应该看看这个。