Created
December 7, 2020 23:05
-
-
Save jzpeepz/15c5155222c63d25c297aa3af622f397 to your computer and use it in GitHub Desktop.
Eloquent Table Prefix for Specific Models
This file contains 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
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; | |
} | |
} |
This file contains 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
class ModelTest extends \Illuminate\Database\Eloquent\Model | |
{ | |
use PilotTablePrefix; | |
protected $guarded = ['id']; | |
} |
This file contains 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
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