Created
November 1, 2016 03:35
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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