Skip to content

Instantly share code, notes, and snippets.

@jzpeepz
Created December 7, 2020 23:05
Show Gist options
  • Save jzpeepz/15c5155222c63d25c297aa3af622f397 to your computer and use it in GitHub Desktop.
Save jzpeepz/15c5155222c63d25c297aa3af622f397 to your computer and use it in GitHub Desktop.
Eloquent Table Prefix for Specific Models
trait HasTablePrefix
{
public function getTable()
{
return $this->getPrefix() . parent::getTable();
}
public function setTable($table)
{
if (substr($table, 0, strlen($this->getPrefix())) == $this->getPrefix()) {
$table = substr($table, strlen($this->getPrefix()));
}
$this->table = $table;
return $this;
}
public function getPrefix()
{
return is_null($this->prefix) ? '' : $this->prefix;
}
public function setPrefix($prefix)
{
$this->prefix = $prefix;
return $this;
}
}
class ModelTest extends \Illuminate\Database\Eloquent\Model
{
use PilotTablePrefix;
protected $guarded = ['id'];
}
trait PilotTablePrefix
{
use HasTablePrefix;
protected $prefix = 'pilot_';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment