Php Laravel-如何质疑雄辩的关系?

Php Laravel-如何质疑雄辩的关系?,php,laravel,laravel-5.8,Php,Laravel,Laravel 5.8,我在Laravel-5.8中有这个模型: class Employee extends Model { public $timestamps = false; protected $table = 'employees'; protected $primaryKey = 'id'; protected $fillable = [ 'id', 'first_name',

我在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('App\Models\Hr\EmployeeType','employee_type_id','id');
    }           
}


class EmployeeType extends Model
{
    public $timestamps = false;
    protected $table = 'employee_types';
    protected $primaryKey = 'id';

    protected $fillable = [
                  'type_name',
                  'is_active',
              ];
}
然后我在Employee controller函数中进行了以下查询:

 $unsubmitted = Employee::where('hr_status', 0)->get();
如何将员工类型中的where is_active=1包含到员工的查询中:

$unsubmitted = Employee::where('hr_status', 0)->get();

您正在查找whereHas方法,查询是否存在关系:

Employee::where('hr_status', 0)
    ->whereHas('employeetype', function ($q) {
        $q->where('is_active', 1);
    })->get();

where has

因此,您希望所有拥有
hr\u status=0
且与任何拥有
is\u active=1
的employeetype有关系的员工都有关系?@lagbox-是的,您是对的。注意:您编写了Laravel 5.8-这是EOL(生命终结)。您应该考虑更新到支持的版本。有关详细信息,请参阅。