I have this models in Laravel-5.8:
class Employee extends Model { public $timestamps = false; protected $table = 'employees'; protected $primaryKey = 'id'; protected $fillable = [ 'id', 'first_name', 'last_name', 'hr_status', 'employee_type_id', ]; public function employeetype() { return $this->belongsTo('AppModelsHrEmployeeType','employee_type_id','id'); } } class EmployeeType extends Model { public $timestamps = false; protected $table = 'employee_types'; protected $primaryKey = 'id'; protected $fillable = [ 'type_name', 'is_active', ]; }
Then I have this Query in Employee controller function:
$unsubmitted = Employee::where('hr_status', 0)->get();
How do I include where is_active = 1 from employee_types into the query in Employee:
$unsubmitted = Employee::where('hr_status', 0)->get();
Advertisement
Answer
You are looking for the whereHas
method, querying for the existence of a relationship:
Employee::where('hr_status', 0) ->whereHas('employeetype', function ($q) { $q->where('is_active', 1); })->get();
Laravel 5.8 Docs – Eloquent – Relationships – Querying Relationship Existence whereHas