You can simply override the table name in your Eloquent Model, example :
protected $table = 'pmt_users';
my database config has the following:
'prefix' => 'pmt_',
if i use this code:
protected $table = 'pmt_users';
will make the table reference as:
'pmt_pmt_users'
i don't want that.
i need soemthing like:
protect $dbPrefixOveride = '';
protect $table = 'users';
to get the table
'users'
and ignore my database.php setup. and if i ommit the $dbPrefixOverride, it uses the default database.php prefix setting.
does this explain it better?
Yes I understand but problem is if you add a prefix in database config, laravel will prepend every database access with it (think of it as 'namespacing' your database). I already experimented quite a lot with it.
What in your user case prevents you from setting the prefix at the model level ?
Alternatively, as you choose which DB connection to use on each model, you could setup a non-prefixed connection in config/database.php along with your prefixed one, and use this connection on non-prefixed models.
I little bit late but my simple solution is encapsulation.
use Illuminate\Database\Eloquent\Model;
define ('MY_PREFIX_CONSTANT','my_prefix');
class UberModel extends Model
{
protected $prefix;
public function __construct(array $attributes = [])
{
if(isset($this->prefix)){
$this->table = $this->prefix.$this->table;
} else {
$this->table = MY_PREFIX_CONSTANT.$this->table;
}
parent::__construct($attributes);
}
}
Now you need to keep in mind to use UberModel. U can just change use declaration to use alias for every model file. Just define $prefix variable in model where you need custom prefix. Otherwise define default prefix in MY_PREFIX_CONSTANT.
imkebe said:
I little bit late but my simple solution is encapsulation.
use Illuminate\Database\Eloquent\Model; define ('MY_PREFIX_CONSTANT','my_prefix'); class UberModel extends Model { protected $prefix; public function __construct(array $attributes = []) { if(isset($this->prefix)){ $this->table = $this->prefix.$this->table; } else { $this->table = MY_PREFIX_CONSTANT.$this->table; } parent::__construct($attributes); } }
Now you need to keep in mind to use UberModel. U can just change use declaration to use alias for every model file. Just define $prefix variable in model where you need custom prefix. Otherwise define default prefix in MY_PREFIX_CONSTANT.
I am having the safe difficulty but on only one relation.. for ex.
i have two tables 1. data_leagues 2. tipp_league_user
i have this relation and the default connection on League model is 'mysql_data' which uses a prefix 'data_'. and a different connection which has tipp_ prefix.
this is the relation in users model on leagues. and i want to refer tipp_league_user as the pivot table but its taking data_league_user.
public function leagues()
{
return $this->belongsToMany('App\League','league_user','user_id','league_id');
}
any fix for this ? or i am doing it wrong ?
Nevermind. I just changed the table name from tipp_league.. to data_tipp_league..
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community