Update the table name at runtime not working - laravel Eloquent ORM

前端 未结 5 1526
借酒劲吻你
借酒劲吻你 2020-12-15 11:15

This is my model:

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

I want to update the table name oops_

5条回答
  •  独厮守ぢ
    2020-12-15 12:00

    Found a way to do this: Inser this into your Model Class and customize to suit 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
          );
        }
    

提交回复
热议问题