Search code examples
phparrayslaravellaravel-5

Relationship Has Many Through without firstKey


is there anyway to get result from hasManythrough relationship without the firstKey?

Right now my result is:

 return $this->hasManyThrough('App\Facility', 'App\MerchantBranchFacility', 'id,'facility_id')->select('name');

    {"facilities":[{"name":"AC","id":"13"},{"name":"Wi-Fi","id":"13"}}}

I realize that inside hasmanythrough method laravel always do this:

  return array_merge($columns, [$this->parent->getTable().'.'.$this->firstKey]);

How can i remove "id" without foreach?


Solution

  • I'm not sure you can set this on a model side, but this:

    $collection->map(function($item) {
        return array_except($item, 'id');
    });