简体   繁体   English

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

[英]How to apply filter with relational table using with statement

I wrote a code to filter with the relational data, but I am not getting a good result.我写了一个代码来过滤关系数据,但我没有得到好的结果。 Here is code.这是代码。 The parent query and with a query is working independently.父查询和带查询是独立工作的。

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;
}

when I used this当我用这个

{
    "search":""
}

I am getting this response:我得到这个回应:

{
    "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"
    }
}

when I tried to search by employee name which is present another table, if the data is present then data should be filtered and give a response but if the employee name or search data is not present then data should not give any response.当我尝试按存在另一个表的员工姓名进行搜索时,如果存在数据,则应过滤数据并给出响应,但如果不存在员工姓名或搜索数据,则数据不应给出任何响应。 How to deal with it?如何处理?

{
    "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
}

here data is null but why parent data is showing.这里数据为空,但为什么显示父数据。 please help and give another idea to filter with relational data.请帮忙并给出另一个想法来过滤关系数据。

we use whereHas to filter in related table.我们使用whereHas在相关表中进行过滤。 so use this as like所以像这样使用它

$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');

this will search in your related table and return null if not found anything.这将在您的相关表中搜索并在未找到任何内容时返回 null。 and i have added elegant way to eager load data.我添加了一种优雅的方式来急切加载数据。 you should check out this too.你也应该看看这个。

use whereHas() function使用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参考链接https://laravel.com/docs/7.x/eloquent-relationships#querying-relationship-existence

it is using whereHas, then With :它使用 whereHas,然后使用:

$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');
    }]);

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

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