php - Laravel dynamic pivot table -


in app use custom class replicate tables during tests. class create new tables _test postfix , tell eloquent work them. when work "many many" relations need specify pivot table name too. possible change pivot table during runtime application?

if have understood question correctly, want able change tables in many-to-many relationship dynamically.

please pay attention source code of belongstomany relationship:

public function belongstomany($related, $table = null, $foreignkey = null, $relatedkey = null, $relation = null) {     // if no relationship name passed, pull backtraces     // name of calling function. use function name     // title of relation since great convention apply.     if (is_null($relation)) {         $relation = $this->guessbelongstomanyrelation();     }      // first, we'll need determine foreign key , "other key"     // relationship. once have determined keys we'll make query     // instances relationship instances need this.     $instance = $this->newrelatedinstance($related);      $foreignkey = $foreignkey ?: $this->getforeignkey();      $relatedkey = $relatedkey ?: $instance->getforeignkey();      // if no table name provided, can guess concatenating 2     // models using underscores in alphabetical order. 2 model names     // transformed snake case default camelcase also.     if (is_null($table)) {         $table = $this->joiningtable($related);     }      return new belongstomany(         $instance->newquery(), $this, $table, $foreignkey, $relatedkey, $relation     ); } 

you can define table there. suggest following:(consider many-to-many relationship between user , company model)

public function users(){      $table = env('app_env') == 'production' ? 'table_name' : 'test_table_name';      $this->belongstomany(user::class, $table); } 

and same company model never tested should work.


Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -