sunfarma hulla
sunfarma hulla

Reputation: 49

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.

Upvotes: 0

Views: 58

Answers (3)

zahid hasan emon
zahid hasan emon

Reputation: 6233

we use whereHas to filter in related table. 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. and i have added elegant way to eager load data. you should check out this too.

Upvotes: 1

Alzafan Christian
Alzafan Christian

Reputation: 589

it is using whereHas, then 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');
    }]);

Upvotes: 0

Kamlesh Paul
Kamlesh Paul

Reputation: 12391

use whereHas() function

  $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

Upvotes: 0

Related Questions