使用Db的写法
$a = Db::name('physical_test_log a')
->join('user b','a.tester_id=b.id')
->join('user c','a.testee_id=c.id')
->join('agency d','a.root_agency_id=d.id')
->join('agency_class e','a.agency_id=e.id')->field('a.id,a.test_date,a.age_year,b.username as tester_name,c.username as testee_name,d.name as agency_name,e.name as class_name')
->select()
->toArray();
使用模型的写法 在model文件里先关联模型
public function tester()
{
return $this->hasOne('User', 'id', 'tester_id')->bind(['tester_name'=>'username']);
}
public function testee()
{
return $this->hasOne('User', 'id', 'testee_id')->bind(['testee_name'=>'username']);
}
public function agency()
{
return $this->hasOne('Agency', 'id', 'root_agency_id')->bind(['agency_name'=>'name']);
}
public function agencyclass()
{
return $this->hasOne('AgencyClass', 'id', 'agency_id')->bind(['class_name'=>'name']);
}
在控制器里
$list = PhysicalTestLog::with(['tester', 'testee', 'agency', 'agencyclass'])
->where($where)
->order($sort, $order)
->limit($offset, $limit)
->select();
$list->visible(['id', 'test_date', 'age_year', 'tester_name', 'testee_name', 'class_name', 'agency_name','agency_id']);
$list = collection($list)->toArray();