Question: I have these 2 linked models: `jobs` and `job_translations`. A job have many translations. So in my job model, there is :

```
/**
 * Get the translations for the job.
 */
public function translations()
{
    return $this->hasMany('App\Models\JobTranslation');
}

```

In my controller, I want to build a query dynamically like that : 

```
$query = Job::query();
if ($request->has('translation')) {
            $query->translations()->where('external_translation', 'ilike', '%'.$request->translation.'%');
        }

$jobs = $query->paginate(10);

```

I have this error : 

> 
> Call to undefined method Illuminate\Database\Eloquent\Builder::translations()
> 
> 
> 

Is it possible to do such a dynamic query with Eloquent?

Answer: 
Yes, it is possible. What you are looking for is `whereHas('translations', $callback)` instead of `translations()`:

```
$query = Job::query();
if ($request->has('translation')) {
    $query->whereHas('translations', function ($query) use ($request) {
        $query->where('external_translation', 'ilike', '%'.$request->translation.'%');
    });
}

$jobs = $query->paginate(10);

```

Your query can be improved further by using `when($condition, $callback)` instead of an `if`:

```
$jobs = Job::query()
    ->when($request->translation, function ($query, $translation) {
        $query->whereHas('translations', function ($query) use ($translation) {
            $query->where('external_translation', 'ilike', "%{$translation}%");
        });
    })
    ->paginate(10);

```
the issue you should do eager loading to detected on next chained query like this:

```
$query = Job::with('translations')->query();

```