I have an issue in below query. The query returns result without running conditions inside eager function. Although we have assigned values for $from_code and $to_code, it doesn't append those inner conditions. it just return all data within empperson table regardless the values assigned $from_code and $to_code .
$empPersons = Empperson::with(['empmaster' => function($query) use ($from_code,$to_code){
if($from_code !=""){
$query->where('empmaster.empCode','>=',$from_code);
}
if($to_code !=""){
$query->where('empmaster.empCode','<=',$to_code);
}
}]);
My Empmaster and Empperson models are as follows.
Empmaster.php
class Empmaster extends Model
{
protected $table = 'empmaster';
public function empPerson()
{
return $this->hasOne('App\Empperson');
}
}
Empperson.php
class Empperson extends Model
{
protected $table = 'empperson';
public function empMaster()
{
return $this->belongsTo('App\Empmaster','empmaster_id','id');
}
}