Laravel-资源获取数组的所有关系

时间:2020-04-14 05:54:07

标签: laravel

我无法获取资源内部数组的所有关系。我想获取与员工设置的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数组中每个项目的所有关系

0 个答案:

没有答案