Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 6 months ago.
0

You can simply override the table name in your Eloquent Model, example :

 protected $table = 'pmt_users';
Last updated 6 months ago.
0

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?

Last updated 6 months ago.
0

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 ?

Last updated 6 months ago.
0

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.

Last updated 6 months ago.
0

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.

Last updated 6 years ago.
0

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 ?

0

Nevermind. I just changed the table name from tipp_league.. to data_tipp_league..

0

Sign in to participate in this thread!

LoadForge

Your banner here too?

sgoodwin10 sgoodwin10 Joined 20 Mar 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2022 Laravel.io - All rights reserved.