我有9张喜欢加入的桌子都有来自员工桌的外键employee_id。我怎样才能获得ORM分发。下面是连接所有功能的DB功能。
$modelEmployee = \DB::table('employees')
->select('*')
->join('employee_finances', 'employees.id', '=', 'employee_finances.employee_id')
->join('employee_addresses', 'employees.id', '=', 'employee_addresses.employee_id')
->join('employee_jobs', 'employees.id', '=', 'employee_jobs.employee_id')
->join('employee_admins', 'employees.id', '=', 'employee_admins.employee_id')
->join('employee_personals', 'employees.id', '=', 'employee_personals.employee_id')
->join('employee_memberships', 'employees.id', '=', 'employee_memberships.employee_id')
->where('employees.id', $id)
->get();
答案 0 :(得分:2)
你可以使用:
$row = $this->model->
where("id",$id)
->with("employee_finances")
->with("employee_addresses")
->with("employee_jobs")
->with("employee_admins")
->with("employee_personals")
->with("employee_memberships")
->with("employee_finances")
->get();
return $row->isEmpty() ? [] : $row->toArray();
plz使用这些名称定义模型中的关系,并使用此处的表单。
答案 1 :(得分:1)
首先,为您的员工表创建模型,并向其他表模型添加各种关系
员工的雄辩模型表
namespace App\Models;
class Employee extends \Illuminate\Database\Eloquent\Model {
public function employee_finances()
{
return $this->hasMany(\App\Models\EmployeeFinance);
}
public function employee_addresses()
{
return $this->hasMany(\App\Models\EmployeeAddress);
}
public function employee_jobs()
{
return $this->hasMany(\App\Models\EmployeeJob);
}
public function employee_admins()
{
return $this->hasMany(\App\Models\EmployeeAdmin);
}
public function employee_personals()
{
return $this->hasMany(\App\Models\EmployeePersonal);
}
public function employee_memberships()
{
return $this->hasMany(\App\Models\EmployeeMembership);
}
}
现在为其他连接表创建模型。以下是 employee_finances 表的示例。 (同样,创建其他模型)
namespace App\Models;
class EmployeeFinance extends \Illuminate\Database\Eloquent\Model {
...
}
然后,对于您的查询,您可以使用查询构建器的与和 whereHas 函数来使用关系。这相当于问题中提到的查询结果,但结果的结构会有所不同;
\App\Models\Employee::with('employee_finances','employee_addresses','employee_jobs','employee_admins','employee_personals','employee_memberships')
->whereId($employeeid)
->whereHas('employee_finances')
->whereHas('employee_addresses')
->whereHas('employee_jobs')
->whereHas('employee_admins')
->whereHas('employee_personals')
->whereHas('employee_memberships')
->get();
原始结果将是常见构建器对象的对象,您无法触发可在模型级别定义的其他关系操作。 原始结果也将是结果的平面数组,可能会少一些。此处的一个示例是 id 列值将替换为主要员工的表列ID。
[
0 => [
'id' => 1,
'employee_name' => 'Employee',
'employee_finance_content' => 'finance_content',
'employee_personal_content' => 'personal_content',
'employee_jobs_content' => 'employee_jobs',
'employee_addresses_content' => 'employee_addresses',
'employee_admins_content' => 'employee_admins',
]
....
]
结果将是员工模型的一个实例。最终结果将是一个关联数组,其中每个关系都是数组的索引,但结果将是相关模型的一个实例,例如,employee_finances将是一个索引或代表一个列,并且包含在其中的值将是 EmployeeFinance 的一个实例,您可以在其上进一步执行ORM级别的活动。
[
0 => [
'id' => 1,
'employee_name' => 'Employee'
'employee_finances' => [
'id' => 2,
'employee_id' => 1,
'employee_finance_content' => 'finance_content'
],
'employee_addresses' => [
'id' => 10,
'employee_id' => 1,
'employee_address_content' => 'employee_address'
]
],
.....
]