我无法获取资源内部数组的所有关系。我想获取与员工设置的departments
相关的所有objectives
。
public function toArray($request)
{
$defaultData = !$this->resource !== null ? parent::toArray($request) : '';
$additionalData = [
// Position is not an array so it is a successful return
'position' => $this->employee !== null ? $this->employee->position->departments : '',
//Property [departments] does not exist on this collection instance.
'objective_department' => $this->employee !== null ? $this->employee->objectives->departments
return array_merge($defaultData, $additionalData);
}
关系
public function departments()
{
return $this->belongsToMany('App\Department');
}
public function objectives()
{
return $this->hasMany('App\Objective', 'department_id');
}
编辑:
我目前从api
获得的数据是
{
id: 1
objectives: [
{
id: 1
department_id: 2
}
{
id: 2
department_id: 2
}
]
}
我想从department_id
获取objectives
数组中每个项目的所有关系