Created
March 2, 2016 04:02
-
-
Save paulofreitas/6f555f7605de6f308883 to your computer and use it in GitHub Desktop.
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
| <?php | |
| /** | |
| * Laravel trait to allow aliasing table fields. | |
| * | |
| * @author Paulo Freitas <[email protected]> | |
| */ | |
| namespace App; | |
| trait Mappable | |
| { | |
| public function getAttribute($key) | |
| { | |
| if (isset($this->mappable) && is_array($this->mappable) | |
| && array_key_exists($key, $this->mappable)) { | |
| return $this->getMappedAttribute($key); | |
| } | |
| return parent::getAttribute($key); | |
| } | |
| public function setAttribute($key, $value) | |
| { | |
| if (isset($this->mappable) && is_array($this->mappable) | |
| && array_key_exists($key, $this->mappable)) { | |
| return $this->setMappedAttribute($key, $value); | |
| } | |
| return parent::setAttribute($key, $value); | |
| } | |
| public function getMappedAttribute($key) | |
| { | |
| return $this->getAttribute($this->mappable[$key]); | |
| } | |
| public function setMappedAttribute($key, $value) | |
| { | |
| return $this->setAttribute($this->mappable[$key], $value); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment