Skip to content

Instantly share code, notes, and snippets.

@paulofreitas
Created March 2, 2016 04:02
Show Gist options
  • Select an option

  • Save paulofreitas/6f555f7605de6f308883 to your computer and use it in GitHub Desktop.

Select an option

Save paulofreitas/6f555f7605de6f308883 to your computer and use it in GitHub Desktop.
<?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