Skip to content

Instantly share code, notes, and snippets.

@timothyallan
Created November 1, 2016 03:35
Show Gist options
  • Save timothyallan/efe3b9c9b3d4eb5cc8ecfc46af79b99f to your computer and use it in GitHub Desktop.
Save timothyallan/efe3b9c9b3d4eb5cc8ecfc46af79b99f to your computer and use it in GitHub Desktop.
Rearrange ouput JSON to eliminate a Laravel 5.x "Pivot" table and just return the single value. Can be used for all sorts of remapping.
public function toArray()
{
$attributes = $this->attributesToArray();
$attributes = array_merge($attributes, $this->relationsToArray());
// Detect if there is a pivot value and return that as the default value
if (isset($attributes['pivot']['value'])) {
$attributes['value'] = $attributes['pivot']['value'];
unset($attributes['pivot']);
}
return $attributes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment