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


tables: contact, company , relationship table custom pivot attribute company_contact (company_id, contact_id, is_main)

company , contact have many many relationship (belongsto on both models).

expected output when retrieve contacts of company:

{     "data": [         {             "id": 1,             "name": "johndoe",             "is_main": false         },         {             "id": 2,             "name": "janedoe",             "is_main": true         }     ] } 

expected output when retrieve contact list ?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                     }                 ]             }         }     ] } 

what's best way of adding pivot table attribute? doesn't seem clean add is_main on company transformer if attribute set.

for first example thinking using parameters ?include=company_relationship:company_id(1) like:

public function includecompanyrelationship(contact $contact, parambag $params) {     // .. retrieve pivot table data here     $is_main = $company->is_main;      // need transformer, when want push value on main array (same level)      return $this->item(??, ??); } 

i understand how retrieve pivot data (related: laravel 5.1 - pivot table between 3 tables, better option?) not best way of adding in https://github.com/thephpleague/fractal transformer logic.

i have contacttransformer , companytransformer if add is_main companytransformer calls make (related or not contacts) expect attribute.

if i'm reading correctly, can utilize single companytransformer handle whether wish have is_main property set, if $contact parameter passed in it's constructor, along these lines:

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) {             // step may not necessary, don't think pivot data              // available on $company object passed in             $company = $this->contacts->find($company->id);             // may have cast boolean if important             $output['is_main'] = $company->pivot->is_main;         }          return $output;     } } 

then in includecompanyrelationship pass in new companytransformer parameter:

public function includecompanyrelationship(contact $contact)  {     $companies = $contact->companies;      return $this->collection($companies, new companytransformer($contact)); } 

this should work whether you're calling companies endpoint directly, or calling contact's endpoint while embedding company relationship data.


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -