3

表:contactcompany以及具有自定义数据透视属性的关系表company_contact (company_id, contact_id, is_main)

公司和联系人具有多对多关系(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
                    }
                ]
            }
        }
    ]
}

添加数据透视表属性的最佳方法是什么?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(??, ??);
}

我了解如何检索数据透视表(相关:Laravel 5.1 - 三个表之间的数据透视表,更好的选择?)但不是在https://github.com/thephpleague/fractal Transformer 逻辑中添加它的最佳方式。

我已经有一个 ContactTransformer 和 CompanyTransformer 但如果我添加is_main到 CompanyTransformer 我所做的所有呼叫(与联系人相关或不与联系人相关)也将期望该属性。

4

2 回答 2

3

如果我没看错的话,您可以使用一个CompanyTransformer来处理您是否希望is_main设置属性,但前提$contact是将参数传递给它的构造函数,类似于以下内容:

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直接调用端点,还是在嵌入公司关系数据时调用联系人的端点,这都应该有效。

于 2015-09-21T14:18:41.007 回答
2

我知道这是旧的,但我刚刚遇到了这个问题。这是我解决它的方法。

在我的情况下添加withPivot了关系categoryusers.

在 CategoryTransformer 中,我定义了我的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课堂上,在transform()方法上:

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

然后,当我调用我的 apicategories并包含时,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该领域。仍然不理想,但在我看来不那么混乱。

于 2016-10-14T14:40:11.103 回答