Laravel 5.1 和分形:包括变压器上的枢轴 table 数据

Laravel 5.1 and Fractal: including pivot table data on the transformer

表:contactcompany 和具有自定义数据透视属性 company_contact (company_id, contact_id, is_main)

的关系 table

公司和联系人具有多对多关系(belongsTo 在两个模型上)。

检索公司联系人时的预期输出:

{
    "data": [
        {
            "id": 1,
            "name": "JohnDoe",
            "is_main": false
        },
        {
            "id": 2,
            "name": "JaneDoe",
            "is_main": true
        }
    ]
}

当我使用 ?include=companies:

检索联系人列表时的预期输出
{
    "data": [
        {
            "id": 1,
            "name": "John Doe",
            "companies": {
                "data": [
                    {
                        "id": 501,
                        "name": "My Company",
                        "is_main": true
                    },
                    {
                        "id": 745,
                        "name": "Another Company",
                        "is_main": false
                    }
                ]
            }
        },
        {
            "id": 2,
            "name": "Jane Doe",
            "companies": {
                "data": [
                    {
                        "id": 999,
                        "name": "Some Company",
                        "is_main": true
                    }
                ]
            }
        }
    ]
}

添加 pivot table 属性的最佳方法是什么?如果设置了属性,在公司变压器上添加is_main似乎不​​是很干净。

对于第一个示例,我正在考虑将参数 ?include=company_relationship:company_id(1) 与类似的东西一起使用:

public function includeCompanyRelationship(Contact $contact, ParamBag $params) {
    // .. retrieve the pivot table data here
    $is_main = $company->is_main;

    // but now I would need another transformer, when what I actually want is to push the value on the main array (same level)

    return $this->item(??, ??);
}

我了解如何检索数据透视数据(相关:) but not the best way of adding it in the https://github.com/thephpleague/fractal 转换器逻辑。

我已经有一个 ContactTransformer 和 CompanyTransformer,但如果我将 is_main 添加到 CompanyTransformer,我进行的所有调用(与联系人相关或不相关)也将需要该属性。

如果我没看错,您可以使用一个 CompanyTransformer 来处理您是否希望设置 is_main 属性,但前提是 [=14] =] 参数被传递给它的构造函数,沿着这些行:

class CompanyTransformer extends TransformerAbstract
{
    public function __construct(Contact $contact = null)
    {
        $this->contact = $contact;
    }

    public function transform(Company $company)
    {
        $output = [
            'id' => $company->id,
            'name' => $company->name,
        ];

        if($this->contact) {
            // This step may not be necessary, but I don't think the pivot data 
            // will be available on the $company object passed in
            $company = $this->contacts->find($company->id);
            // You may have to cast this to boolean if that is important
            $output['is_main'] = $company->pivot->is_main;
        }

        return $output;
    }
}

然后在你的 includeCompanyRelationship 中传入新的 CompanyTransformer 参数:

public function includeCompanyRelationship(Contact $contact) 
{
    $companies = $contact->companies;

    return $this->collection($companies, new CompanyTransformer($contact));
}

无论您是直接调用 companies 端点,还是在嵌入公司关系数据时调用联系人的端点,这都应该有效。

我知道这是旧的,但我只是 运行 进入这个问题。这是我解决它的方法。

withPivot 添加到关系中,在我的例子中是 categoryusers

在 CategoryT运行sformer 中,我定义了我的 includeUsers 方法:

/**
 * Include Users
 * @param Category $category
 * @return \League\Fractal\Resource\Collection
 */
 public function includeUsers(Category $category)
 {
     # Just Add withPivot Here.
     $users = $category->users()->withPivot('role')->get();
     return $this->collection($users, new UserTransformer);
 }

然后在 UserTransformer class 中,在 transform() 方法中:

public function transform($user)
{
    return [
        'username' => $user['username'],
        'lastname' => $user['lastname'],
        // ... truncated
        'role' => isset($user['pivot']) ? $user['pivot']['role'] : null,
    ]
}

然后,当我为 categories 调用 api 并包含 users 时,我得到:

{
  "data": [
    {
      "name": "Network",
      "description": "some description of category here.",
      "users": {
        "data": [
          {
            "id": 1,
            "username": "jborne",
            "firstname": "Jason",
            "lastname": "Borne",
            "title": "Department Head",
            "company": "Borne Inc",
            "email": "jason@somedomain.com",
            "display_name": "Jason Borne",
            "mobile": "555-5555",
            "active": true,
            "role": "IM",
          }
        ]
      }
    }
  ]
}

如您所见,我从枢轴得到了我想要的 role 关系。否则,您只会得到该字段的 null。仍然不理想,但在我看来不那么混乱了。