Update table name at run time without working - laravel Eloquent ORM

This is my model:

class Product extends \GlobalModel { protected $table = 'product'; } 

I want to update the table name oops_product instead of the product at runtime.

I found getTable (); To get the table name from the model and its performance:

 $tableName = with(new Product)->getTable(); 

But when I set the table name using setTable (); according to github , its not updating the table name.

 with(new Product)->setTable("oops_produdct"); 

Is there something wrong?

Help will be appreciated.

edited by:

 $product = new Product(); $product->getTable(); // output: product $product->setTable("oops_product"); $product->getTable(); // output: oops_product 

now when i run this

 $product->all(); 

he performs

 "select * from product" 

instead

 "select * from oops_product" 
+8
source share
5 answers

all() is a static method that uses a new instance and calls get() on it.

So all you need is the correct method:

 $product = new Product; $product->getTable(); // products $product->setTable('oooops'); $product->get(); // select * from oooops $product->first(); // select * from oooops limit 1 etc... 

Just avoid using static Eloquent methods, as they obviously create a new instance that will have the table property by default.

+15
source

The problem with the accepted answer is that changing the extracted instance of the model and then saving it will not work. See my comment above.

The following feature allows you to pass the table name during hydration.

 trait BindsDynamically { protected $connection = null; protected $table = null; public function bind(string $connection, string $table) { $this->setConnection($connection); $this->setTable($table); } public function newInstance($attributes = [], $exists = false) { // Overridden in order to allow for late table binding. $model = parent::newInstance($attributes, $exists); $model->setTable($this->table); return $model; } } 

Here's how to use it:

 class Product extends Model { use BindsDynamically; } 

Applies to accepted answer:

 $product = new Product; $product->getTable(); // products $product->bind('connection', 'oooops'); $product->get(); // select * from oooops $product->first(); // select * from oooops limit 1 etc... $product->myTestProp = 'test; $product->save(); // now saves into oooops 
+5
source

Found a way to do this: Insert this into your model class and customize according to your logic.

  public function companies(){ $instance = Community::of($this->community); //create instance $foreignKey = 'id'; $localKey = 'foreign_id'; $tableName = $instance->getTable(); //or any logic of yours //Here you can dynamically choose the table name, taken from the "HasRelationships.php" File of Laravel return $this->newHasMany( $instance->newQuery(), $this, $tableName.'.'.$foreignKey, $localKey ); } 
0
source

this should work with static methods (checked only on 5.7)

 use Illuminate\Database\Eloquent\Model; function makeModel($table) { $model = new class extends Model { public static $entryTable; public function setTable($table) { $this->table = static::$entryTable = $table; } public static function __callStatic($method, $parameters) { $static = (new static); $static->setTable($static::$entryTable); return $static->$method(...$parameters); } }; $model->setTable($table); return $model; } 
0
source

I needed to change the name of the table based on my query (my tables will have a prefix based on my query). I extended the Model class to MyModel and then redefined the construct method, combining the table name based on my query and the default table name provided by my models.

 class MyModel extends Model { public function __construct(array $attributes = []) { parent::__construct($attributes); $this->setTable(Config::get('tablePrefix').$this->getTable()); } } 

which you should replace with the configuration table prefix for your approach.

and then in my models I just extended MyModel and everything was fine, including static calls, as I tested.

 class Category extends MyModel { protected $table = "categories"; } 

Therefore, when my query changed, I changed the configuration I provided and was able to get different table names, such as a__categories, b__categories, and everything was in order, including static calls, relationships and saving to the database.

0
source

Source: https://habr.com/ru/post/979441/


All Articles