Laravel分组依据,在不同表中具有关系

Geo*_*off 1 php laravel

我正在雄辩地使用laravel,试图获取包含在不同表中的信息并进行比较。

我的桌子是

tbl_checks
    id,check_id, person_id //person_id is the foreign key

tbl_persons
   id, category, 
Run Code Online (Sandbox Code Playgroud)

我想获取表中的所有人员,tbl_checks然后按人员类别对数据进行分组tbl_persons.category

在普通的SQL语句中,它类似于:

select from tbl_checks group by tbl_person.category where the tbl_persons.id is contained in tbl_checks.person_id
Run Code Online (Sandbox Code Playgroud)

在我的模型中,我有:

class PersonsModel extends Model {
    //connect to the next database
    protected $connection = 'inspection';
    protected $table = 'tbl_trucks';

    //relation with tbl_checks

    public function checks(){
        return $this->hasMany('App\TblChecks','person_id','id');
    }
}
Run Code Online (Sandbox Code Playgroud)

如何使用ELoquent模型,而不是Db门面?为了使它清晰。我试图找到所有在table_checks中的人,而忽略不在tbl_checks中的人id

Jon*_*eir 12

假设您personTblChecks模型中具有关系:

TblChecks::with('person')->get()->groupBy('person.category');
Run Code Online (Sandbox Code Playgroud)