-2

I have two tables. codes and translations.

codes table

id | name
---|------
1  | A99

translations table

id | code_id | language_code | copy
---|---------|---------------|------
1  |1        |en             | Heart Disease
2  |1        |fr             | Sning Nat

Code model

class Code
{
  function translation() // for default lanuage
  {
   return $this->hasOne(Translation::class, 'code_id')->where('language_code', 'en);
  }
}

Query

$codes = Codes::with(['translation' => function($query){
 $query->where('copy', 'like', '%hltad la loq%');
}]);

This should return nothing, but it returns all Codes without translation.

In other words I just want this query behind the scene.

select codes.*, translations.copy from codes
inner join translations ON codes.id = translations.code_id
where translations.language_code='en'
and translations.copy like '%hltad la loq%'
Shahid Karimi
  • 4,096
  • 17
  • 62
  • 104

1 Answers1

-1

You are using with so it is returning first table it is like left join. If you want to get inner join you should do like this:

$codes = Codes::whereHas('translation', function ($join) {
    return $join->where('copy', 'like', '%hltad la loq%');
})->get();
train_fox
  • 1,517
  • 1
  • 12
  • 31